How to Capture error log using C#.Net?

Discussion in 'C#' started by sameer_havakajoka, Jan 21, 2010.

  1. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me

    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:
    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;
    		} 
    	}
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,358
    Likes Received:
    383
    Trophy Points:
    83
    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.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,358
    Likes Received:
    383
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice