Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/articles/c-sharp-tutorials/)
-   -   How to Capture error log using C#.Net? (http://www.go4expert.com/articles/capture-error-log-using-c-sharpnet-t20718/)

sameer_havakajoka 21Jan2010 09:50

How to Capture error log using C#.Net?
 

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 23Jan2010 11:56

Re: How to Capture error log using C#.Net?
 
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 6Feb2010 09:28

Re: How to Capture error log using C#.Net?
 
Nominate this Article for Article of the month - Jan 2010


All times are GMT +5.5. The time now is 19:42.