How to Capture error log using C#.Net?

sameer_havakajoka's Avatar author of How to Capture error log using C#.Net?
This is an article on How to Capture error log using C#.Net? in C#.

Introduction



How to capture the errors from application and write it in a log file. We have to mention the directory path in Webconfig file.

Background



This code is to capture the error log in log file with all information.

The code



Code: CSharp
public class LogError
{
    private DateTime errorDt;
    private string src;
    private Exception errorInfo;
    public static string strDirectoryPath;
    public DateTime ErrorDate
    {
        get {return errorDt; }
        set { errorDt = value;}
    }
    public string ErrorSrc
    {
        get { return src; }
        set { src = value; }
    }
    public Exception ErrorInformation
    {
        get { return errorInfo; }
        set { errorInfo = value; }
    }
 
    public static void Log_Err(string strErrorSource, Exception Ex)
    {
        LogError errInfo = new LogError();
        errInfo.ErrorDate = System.DateTime.Now;
        errInfo.ErrorSrc = strErrorSource;
        errInfo.ErrorInformation = Ex;
        LogError.LogErr(errInfo);
    }
 
 
    public static void LogErr(LogError errorDTO)
    {
        try
        {
            string directoryPath = strDirectoryPath;
            if (!string.IsNullOrEmpty(strDirectoryPath))
            {
                string path = directoryPath + "\\" + "ErrorLog.txt";
                StreamWriter swErrorLog = null;
                DirectoryInfo dtDirectory = null;
                if (!Directory.Exists(directoryPath))
                {
                    dtDirectory = Directory.CreateDirectory(directoryPath);
                    dtDirectory = null;
                }
                if (File.Exists(path))
                {
                    swErrorLog = new StreamWriter(path, true); //append the error message
                    swErrorLog.WriteLine("Date and Time of Exception: " + errorDTO.ErrorDate);
                    swErrorLog.WriteLine("Source of Exception: " + errorDTO.ErrorSrc);
                    swErrorLog.WriteLine(" ");
                    swErrorLog.WriteLine("Error Message: " + errorDTO.ErrorInformation);
                    swErrorLog.WriteLine("------------------------------------------- ");
                    swErrorLog.WriteLine(" ");
                    //swErrorLog.WriteLine(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                    swErrorLog.Close();
                    swErrorLog = null;
                }
                else
                {
                    swErrorLog = File.CreateText(path);
                    swErrorLog = new StreamWriter(path, true); //append the error message
                    swErrorLog.WriteLine("Date and Time of Exception: " + errorDTO.ErrorDate);
                    swErrorLog.WriteLine("Source of Exception: " + errorDTO.ErrorSrc);
                    swErrorLog.WriteLine(" ");
                    swErrorLog.WriteLine("Error Message: " + errorDTO.ErrorInformation);
                    swErrorLog.WriteLine("------------------------------------------- ");
                    swErrorLog.WriteLine(" ");
                    swErrorLog.Close();
                    swErrorLog = null;
                }
            }
        }
        catch (NullReferenceException)
        {
            throw;
        }
    }
}
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
The code you submitted had a missing braces. Please make sure you post the only right code or else it would not be possible to approve your article now.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nominate this Article for Article of the month - Jan 2010