Error Logging

Error Logging using c# This article is demonstrating how to create error log file using c#. This program will create the text file and log the errors. This class demonstrates the use of following namespaces System.IO and System.Text. The namespaces contain the methods to create files in the directories. The main idea is create a class, AuditLog , that will create files in a specified directory. Public class AuditLog { #region Static variables, Constructor static FileStream fstream

Error Logging using c#

This article is demonstrating how to create error log file using c#. This program will create the text file and log the errors. This class demonstrates the use of following namespaces System.IO and System.Text. The namespaces contain the methods to create files in the directories. The main idea is create a class, AuditLog , that will create files in a specified directory.

 

Public class AuditLog

{

#region Static variables, Constructor

        static FileStream fstream;

        static StreamWriter streamLogWriter;

 

//Here E means entry in the log file, D is for Display

        public enum LOGTYPE

        {

            E,

            D

        }

 

 

        static Audit()

            {

            }

#endregion

 

#region Public Methods

 

//Writelog method is overload method, internaly calling the another writelog //method.

public static void WriteLog(LOGTYPE LogType, int LogSeverity, string UserName, string SourceId, string LogMessage, string Parameters)

        {

            try

            {

                string STRLOGGINGLEVEL = string.Empty;

                string STRLOGGING = string.Empty;

string STRLOG = FormLogString(LogType.ToString(), LogSeverity.ToString(), UserName, SourceId, LogMessage);

               

//Web.config entry <add key="LoggingLevel" value="E"/>

  STRLOGGINGLEVEL = ConfigurationSettings.AppSettings["LoggingLevel"];

//Web.config entry <add key="Logging" value="BOTH"/>

  STRLOGGING = ConfigurationSettings.AppSettings["Logging"];

               

                switch(STRLOGGING)

                {

                   

                    case "BOTH":

                        if (STRLOGGINGLEVEL == "E")

                        {

                            if (LogType.ToString() == "E")

                            {

 

                                WriteLog(STRLOG);

                            }

                        }

                       

                       

                    break;

                }

 

            }

            catch (Exception ex)

            {

               

                throw new Exception(ex.Message, ex.InnerException);

            }

        }

#endregion

 

 

 

 

 

#region Private Members

 

//This method concatinating the log string

private static string FormLogString(string LogType, string LogSeverity, string UserName, string SourceId, string LogMessage)

        {

            string STRLOGSTRING = "\"" + System.DateTime.Now + "\"" + " " +

                                  "\"" + LogType + "\"" + " " +

                                  "\"" + LogSeverity + "\"" + " " +

                                  "\"" + UserName + "\"" + " " +

                                  "\"" + SourceId + "\"" + " " +

                                  "\"" + LogMessage + "\"";

            return STRLOGSTRING;

        }

     

      //Closing the streamlogwriter object

        private static bool CloseFile()

        {

            try

            {

                streamLogWriter.Flush();

                streamLogWriter.Close();

            }

            catch (Exception)

            {

                return false;

            }

            return true;

        }

//This Method to write error message into the log file

//return type is bool

private static bool WriteLog(string strMessage)

        {

            try

            {

                string strLogPath = string.Empty;

                string strLogFileName = string.Empty;

//You can specify the any log path

// web.config entry <add key="LogPath" value="D:\Applications\App\Web\Logs"/>

                strLogPath = ConfigurationSettings.AppSettings["LogPath"];

strLogFileName = "Test_" + DateTime.Today.ToString("ddMMMyyyy") + "_Log.Txt";

 

                StreamWriter sw = null;

 

                if (!File.Exists(strLogPath + "/" + strLogFileName))

                {

                    // Create a file to write to.

                    sw = File.CreateText(strLogPath + "/" + strLogFileName);

                }

                else

                {

                    sw = File.AppendText(strLogPath + "/" + strLogFileName);

                }

 

            string ReadString = System.DateTime.Now + " " + strMessage + " ";

 

                sw.WriteLine(ReadString);

               

                sw.Flush();

                sw.Close();

                return true;

            }

            catch (Exception ex)

            {

                return false;

            }

            finally

            {

            }

        }

        #endregion

 

}

 

 

Calling the auditlog class

string STRPAGEID = "VENDDPM";

Try

{

}

Catch(Exception ex)

{

            AuditLog.WriteLog(AuditLog.LOGTYPE.E, 3, "", STRPAGEID,

"Page Load:Error Messge:" + ex.Message, "");

}

By Rao B   Popularity  (1400 Views)