Creating Error log

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.Data.SqlClient;
using CommonComponents;
using System.IO;

///
/// Summary description for clsErrorLog
///

public class clsErrorLog
{
    /*************************************************************
    NAME:          WriteToErrorLog
    PURPOSE:       Open or create an error log and submit error message
    PARAMETERS:    ex - object of Exception class
                   ErrorTitle - title of the error
                   ErrorClass - name of form or class in which error occured        
                   ErrorMethod - name of the method in which error occured
    RETURNS:       Nothing       
    '*************************************************************/
    ///
    /// Open or create an error log and submit error message. Returns void
    ///

    ///
object of Exception class
    /// title of the error
    /// name of form or class in which error occured
    /// name of the method in which error occured
    ///
    public static void WriteToErrorLog(Exception ex, string ErrorTitle, string ErrorClass, string ErrorMethod)
    {
        /* check and make the directory if necessary; this is set to look in
           the application folder, you may wish to place the error log in
           another location depending upon the user's role and write access to
           different areas of the file system */
        string strPath = HttpContext.Current.Server.MapPath("~\\Errors");
        //if (!Directory.Exists(strPath))
        //    Directory.CreateDirectory(strPath);
        DirectoryInfo objDirInfor = new DirectoryInfo(strPath);
        if (!objDirInfor.Exists)
            objDirInfor.Create();
        //check the file
        FileStream fs = new FileStream(strPath + "\\MilesSelfTestWebErrorlog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        StreamWriter s = new StreamWriter(fs);
        s.Close();
        fs.Close();
        //log it
        FileStream fs1 = new FileStream(strPath + "\\MilesSelfTestWebErrorlog.txt", FileMode.Append, FileAccess.Write);
        StreamWriter s1 = new StreamWriter(fs1);
        System.Text.StringBuilder strErrorDesc = new System.Text.StringBuilder("");
        strErrorDesc.AppendLine("Title: " + ErrorTitle + Environment.NewLine);
        strErrorDesc.AppendLine("ErrorClass: " + ErrorClass + Environment.NewLine);
        strErrorDesc.AppendLine("ErrorMethod: " + ErrorMethod + Environment.NewLine);
        strErrorDesc.AppendLine("Message: " + ex.Message + Environment.NewLine);
        strErrorDesc.AppendLine("StackTrace: " + ex.StackTrace + Environment.NewLine);
        strErrorDesc.AppendLine("Date/Time: " + DateTime.Now.ToString() + Environment.NewLine);
        strErrorDesc.AppendLine("================================================" + Environment.NewLine);
        s1.Write(strErrorDesc.ToString());
        s1.Close();
        fs1.Close();
        //clsEmail objSimpleEmail = new clsEmail();
        //objSimpleEmail.SendEMail("admin@nigeriaexamsonline.com", "bhaveshp@iprogrammer.co.in, milindm@iprogrammer.co.in", ErrorTitle, strErrorDesc.ToString());
        //objSimpleEmail = null;
    }
}

Comments

Popular posts from this blog

How to get motherboard serial number, Processor ID, VolumeSerialNumber using C#?

Fiserv Interview Questions.

AngularJs - Simple Example