Thursday, 14 June 2012

Maintaining Error_logs in ASP.Net using c#

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Web.Mail;
using System.Text;
using System.IO;
using System.Globalization;
/// <summary>
/// Summary description for EventLogger
/// </summary>
public class EventLogger
{
    private static FileStream fileNames;
 public EventLogger()
 {
  //
  // TODO: Add constructor logic here
  //
 }
    #region Public
  
    /// <summary>
    /// Write error into a text file
    /// </summary>
    /// <param name="errorMessage">Datatype - string</param>
    public static void WriteError(string errorMessage)
    {
        try
        {
            if (!(Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("Error"))))
            {
                Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("Error"));
            }
            string path = ConfigurationSettings.AppSettings["ErrorLogPath"].ToString() + DateTime.Today.ToString("dd-MM-yy") + ".txt";
            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
            {
                File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
            }
            using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
            {
                w.WriteLine("\r\nLog Entry : ");
                w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
                              ". Error Message:" + errorMessage;
                w.WriteLine(err);
                w.WriteLine("__________________________");
                w.Flush();
                w.Close();
            }
        }
        catch (Exception ex)
        {
            WriteError(ex.Message);
            EventLogger.WriteError(ex.Message);
        }
    }
    public static void writeError(Exception ex,string Date)
    {
        StackTrace stackTrace = new StackTrace(ex, true);
                      
        string fileNames = stackTrace.GetFrame((stackTrace.FrameCount - 1)).GetFileName();
        //fileNames = fileNames.Substring(fileNames.LastIndexOf(Application.ProductName));
        Int32 lineNumber = stackTrace.GetFrame((stackTrace.FrameCount - 1)).GetFileLineNumber();
      
        if (!(Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("Error"))))
        {
            Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("Error"));
        }
        string path = ConfigurationSettings.AppSettings["ErrorLogPath"].ToString() + DateTime.Today.ToString("dd-MM-yy") + ".txt";
        if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
            File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
        }
        using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
            w.WriteLine("\r\nLog Entry : ");
            w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
            string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() + "Line Number  :" + lineNumber.ToString() +
                          ". Method:" + stackTrace.GetFrame((stackTrace.FrameCount - 1)).GetMethod() + "Error Message :" + ex.Message;
            w.WriteLine(err);
            w.WriteLine("__________________________");
            w.Flush();
            w.Close();
        }
    }
    #endregion
}

No comments:

Post a Comment