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;
        } 
    }

the original topic: http://www.arcoders.net/Forums/Thread.aspx?pageid=2&mid=5&ItemID=2&thread=83

I use NLOG. It's free and does more than you could ask for.

.Net framework offers Trace-functionality. When you want to log some within your code you call:

System.Diagnostics.Trace.Write("message");
//or
System.Diagnostics.Trace.WriteLine("message");
//or
System.Diagnostics.Trace.WriteIf(TraceSwitch, "message");
//or
System.Diagnostics.Trace.WriteLineIf(TraceSwitch, "message");

The TraceSwitch catches the loglevel, with the properties Trace.... (e.g TraceInfo).
The TraceSwitch and the tracelistner can be configured in your config file.
The tracelistner catches your trace events and handle them, e.g. write them into a file.

See also:
http://msdn.microsoft.com/en-us/library/36hhw2t6%28v=VS.71%29.aspx
and
http://msdn.microsoft.com/en-us/library/system.diagnostics.traceswitch.aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.