Error Log Maintain Using C# - OmIndia

Teach To India

Sunday, September 17, 2017

Error Log Maintain Using C#

How To Maintain Error Log in Asp.Net Using C# 


  1. It is easy to maintain error in a notepad (.txt) file.
  2. First of all, we need to create a .txt file to store error.
  3. Error Store with Date and Time.
  4. when the error occurred then using a function we pass that error message to the log file.
  5. This is possible using file stream (Read / Writer).
Now look The Code To Archive this task.

private void LogError(Exception ex)
{
    string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
    message += Environment.NewLine;
    message += "-----------------------------------------------------------";
    message += Environment.NewLine;
    message += string.Format("Message: {0}", ex.Message);
    message += Environment.NewLine;
    message += string.Format("StackTrace: {0}", ex.StackTrace);
    message += Environment.NewLine;
    message += string.Format("Source: {0}", ex.Source);
    message += Environment.NewLine;
    message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
    message += Environment.NewLine;
    message += "-----------------------------------------------------------";
    message += Environment.NewLine;
    string path = Server.MapPath("~/ErrorLog/ErrorLog.txt");
    using (StreamWriter writer = new StreamWriter(path, true))
    {
        writer.WriteLine(message);
        writer.Close();
    }
}


This Code For C# Developer.
Save error in a notepad file.



 

No comments:

Post a Comment

Comments

Popular