RSS LOGORss   Welcome Guest  Login Register
Welcome to our Blogs
Back Back
Subject


mag
2008/06/08 02:48PM
Exception Logger class      Number of views: 2005

 

Exception Logger, this was implemented on one of my projects
I admit I didn't write it all, it was a training for one of my team
and they did the job well, of course, with some adjustments, it's now
fully working.
<--- Starts here --->
public class Logger
    {
        private static string LogFileName = @"C:\..\Logs\WFLogFile.xml";
        private static string LogPath = @"C:\..\Logs";

        public static void LogException(Exception ex)
        {

            CreateExFile();
            string strFilename = LogFileName;

            XmlDocument XMLDom = new XmlDocument();
            XMLDom.Load(strFilename);

            XmlElement parentNode = XMLDom.CreateElement("ExceptionInfo");
            parentNode.SetAttribute("Exception_PageName", "");
            XMLDom.DocumentElement.PrependChild(parentNode);

            XmlElement DateTimeNode = XMLDom.CreateElement("DateTime");
            XmlElement MessageNode = XMLDom.CreateElement("Message");
            XmlElement SourceNode = XMLDom.CreateElement("Source");
            XmlElement StackTraceNode = XMLDom.CreateElement("StackTrace");

            XmlText DateTimeText = XMLDom.CreateTextNode(DateTime.Now.ToString());
            XmlText MessageText = XMLDom.CreateTextNode(ex.Message);
            XmlText SourceText = XMLDom.CreateTextNode(ex.Source);
            XmlText StackTraceText = XMLDom.CreateTextNode(ex.StackTrace);

            parentNode.AppendChild(DateTimeNode);
            parentNode.AppendChild(MessageNode);
            parentNode.AppendChild(SourceNode);
            parentNode.AppendChild(StackTraceNode);

            DateTimeNode.AppendChild(DateTimeText);
            MessageNode.AppendChild(MessageText);
            SourceNode.AppendChild(SourceText);
            StackTraceNode.AppendChild(StackTraceText);

            Exception exCheck = ex;
            int i = 0;

            if (exCheck.InnerException != null)
            {
                XmlElement InnerExceptionsNode = XMLDom.CreateElement("InnerExceptions");

                while (exCheck.InnerException != null)
                {
                    i = i + 1;
                    XmlElement InnerExceptionParentNode = XMLDom.CreateElement("InnerException" + i.ToString());

                    XmlElement InMessageNode = XMLDom.CreateElement("InMessage");
                    XmlElement InSourceNode = XMLDom.CreateElement("InSource");
                    XmlElement InStackTraceNode = XMLDom.CreateElement("InStackTrace");


                    XmlText InMessageText = XMLDom.CreateTextNode(exCheck.Message);
                    XmlText InSourceText = XMLDom.CreateTextNode(exCheck.Source);
                    XmlText InStackTraceText = XMLDom.CreateTextNode(exCheck.StackTrace);

                    InnerExceptionParentNode.AppendChild(InMessageNode);
                    InnerExceptionParentNode.AppendChild(InSourceNode);
                    InnerExceptionParentNode.AppendChild(InStackTraceNode);

                    InMessageNode.AppendChild(InMessageText);
                    InSourceNode.AppendChild(InSourceText);
                    InStackTraceNode.AppendChild(InStackTraceText);

                    InnerExceptionsNode.AppendChild(InnerExceptionParentNode);
                    parentNode.AppendChild(InnerExceptionsNode);
                    exCheck = exCheck.InnerException;
                }
            }

            XMLDom.Save(strFilename);

        }

        public static void WriteNewExToFile(Exception ex, string PageName)
        {

            CreateExFile();
            string strFilename = LogFileName;



            XmlDocument XMLDom = new XmlDocument();
            XMLDom.Load(strFilename);

            XmlElement parentNode = XMLDom.CreateElement("ExceptionInfo");
            parentNode.SetAttribute("Exception_PageName", PageName);
            XMLDom.DocumentElement.PrependChild(parentNode);

            XmlElement DateTimeNode = XMLDom.CreateElement("DateTime");
            XmlElement MessageNode = XMLDom.CreateElement("Message");
            XmlElement SourceNode = XMLDom.CreateElement("Source");
            XmlElement StackTraceNode = XMLDom.CreateElement("StackTrace");

            XmlText DateTimeText = XMLDom.CreateTextNode(DateTime.Now.ToString());
            XmlText MessageText = XMLDom.CreateTextNode(ex.Message);
            XmlText SourceText = XMLDom.CreateTextNode(ex.Source);
            XmlText StackTraceText = XMLDom.CreateTextNode(ex.StackTrace);

            parentNode.AppendChild(DateTimeNode);
            parentNode.AppendChild(MessageNode);
            parentNode.AppendChild(SourceNode);
            parentNode.AppendChild(StackTraceNode);

            DateTimeNode.AppendChild(DateTimeText);
            MessageNode.AppendChild(MessageText);
            SourceNode.AppendChild(SourceText);
            StackTraceNode.AppendChild(StackTraceText);

            Exception exCheck = ex;
            int i = 0;

            if (exCheck.InnerException != null)
            {
                XmlElement InnerExceptionsNode = XMLDom.CreateElement("InnerExceptions");

                while (exCheck.InnerException != null)
                {
                    i = i + 1;
                    XmlElement InnerExceptionParentNode = XMLDom.CreateElement("InnerException" + i.ToString());

                    XmlElement InMessageNode = XMLDom.CreateElement("InMessage");
                    XmlElement InSourceNode = XMLDom.CreateElement("InSource");
                    XmlElement InStackTraceNode = XMLDom.CreateElement("InStackTrace");


                    XmlText InMessageText = XMLDom.CreateTextNode(exCheck.Message);
                    XmlText InSourceText = XMLDom.CreateTextNode(exCheck.Source);
                    XmlText InStackTraceText = XMLDom.CreateTextNode(exCheck.StackTrace);

                    InnerExceptionParentNode.AppendChild(InMessageNode);
                    InnerExceptionParentNode.AppendChild(InSourceNode);
                    InnerExceptionParentNode.AppendChild(InStackTraceNode);

                    InMessageNode.AppendChild(InMessageText);
                    InSourceNode.AppendChild(InSourceText);
                    InStackTraceNode.AppendChild(InStackTraceText);

                    InnerExceptionsNode.AppendChild(InnerExceptionParentNode);
                    parentNode.AppendChild(InnerExceptionsNode);
                    exCheck = exCheck.InnerException;
                }
            }

            XMLDom.Save(strFilename);

        }

        private static void CreateExFile()
        {

            string strFilename = LogFileName;
            bool AddDate = (LogFileName.Contains("WFLogFile.xml"));

            if (!File.Exists(strFilename))
            {
                if (!System.IO.Directory.Exists(LogPath ))
                    System.IO.Directory.CreateDirectory(LogPath );
                if (AddDate)
                {
                    DateTime dt = DateTime.Now;
                    strFilename = strFilename.Replace(".xml", dt.Day.ToString("00") + "-" + dt.Month.ToString("00") + "-" + dt.Year.ToString("0000") + ".xml");
                    LogFileName = strFilename;
                }
                XmlTextWriter newXmlFile = new XmlTextWriter(strFilename, System.Text.Encoding.Default);


                newXmlFile.WriteStartDocument(true);
                newXmlFile.WriteStartElement("ExceptionErrors");
                newXmlFile.WriteEndElement();
                newXmlFile.Flush();
                newXmlFile.Close();
            }
        }
    }
<--- Ends here --->
MAG
Feedbacks
 
m.taha Exception Logger class 13/07/2008 04:09 PM

Thanks MAG for this thread. I really like the idea of logging the exception data into a structured XML file.

I want to share an idea that might be useful some times. Some times, the nesting level of the exceptions might exceed 2 (i.e. The inner exception might contain another inner exception). So I suggest to recursively call LogException recursively passing ex.InnerException as a parameter given it is not null. I used a similar idea in one of the projects that I worked on. It works just fine.

 
mag Exception Logger class 14/07/2008 01:57 AM

 Dear Mohamed,

Please have a look at this while loop

while (exCheck.InnerException != null)
                {
                    i = i + 1;
                    XmlElement InnerExceptionParentNode = XMLDom.CreateElement("InnerException" + i.ToString());

                    ...................

                    InnerExceptionsNode.AppendChild(InnerExceptionParentNode);
                    parentNode.AppendChild(InnerExceptionsNode);
                    exCheck = exCheck.InnerException;
                }

so, there is somekind of recursion, if you need any further details please, let me know.

 

Thank you so much for your interest, and I hope you find the information here useful and informative.

 
mag Exception Logger class 24/07/2008 10:48 AM

Hi, this is the VB.NET version of the code converted by http://www.kamalpatel.net/ConvertCSharp2VB.aspx

C# to VB.NET Converter

<--- Starts here --->

Public Class Logger
        Private Shared LogFileName As String =  "C:\..\Logs\WFLogFile.xml"
        Private Shared LogPath As String =  "C:\..\Logs"
 
        Public Shared  Sub LogException(ByVal ex As Exception)
 
            CreateExFile()
            Dim strFilename As String =  LogFileName
 
            Dim XMLDom As XmlDocument =  New XmlDocument()
            XMLDom.Load(strFilename)
 
            Dim parentNode As XmlElement =  XMLDom.CreateElement("ExceptionInfo")
            parentNode.SetAttribute("Exception_PageName", "")
            XMLDom.DocumentElement.PrependChild(parentNode)
 
            Dim DateTimeNode As XmlElement =  XMLDom.CreateElement("DateTime")
            Dim MessageNode As XmlElement =  XMLDom.CreateElement("Message")
            Dim SourceNode As XmlElement =  XMLDom.CreateElement("Source")
            Dim StackTraceNode As XmlElement =  XMLDom.CreateElement("StackTrace")
 
            Dim DateTimeText As XmlText =  XMLDom.CreateTextNode(DateTime.Now.ToString())
            Dim MessageText As XmlText =  XMLDom.CreateTextNode(ex.Message)
            Dim SourceText As XmlText =  XMLDom.CreateTextNode(ex.Source)
            Dim StackTraceText As XmlText =  XMLDom.CreateTextNode(ex.StackTrace)
 
            parentNode.AppendChild(DateTimeNode)
            parentNode.AppendChild(MessageNode)
            parentNode.AppendChild(SourceNode)
            parentNode.AppendChild(StackTraceNode)
 
            DateTimeNode.AppendChild(DateTimeText)
            MessageNode.AppendChild(MessageText)
            SourceNode.AppendChild(SourceText)
            StackTraceNode.AppendChild(StackTraceText)
 
            Dim exCheck As Exception =  ex
            Dim i As Integer =  0
 
            If Not exCheck.InnerException Is Nothing Then
                Dim InnerExceptionsNode As XmlElement =  XMLDom.CreateElement("InnerExceptions")
 
                While Not exCheck.InnerException Is Nothing
                    i = i + 1
                    Dim InnerExceptionParentNode As XmlElement =  XMLDom.CreateElement("InnerException" + i.ToString())
 
                    Dim InMessageNode As XmlElement =  XMLDom.CreateElement("InMessage")
                    Dim InSourceNode As XmlElement =  XMLDom.CreateElement("InSource")
                    Dim InStackTraceNode As XmlElement =  XMLDom.CreateElement("InStackTrace")
 
 
                    Dim InMessageText As XmlText =  XMLDom.CreateTextNode(exCheck.Message)
                    Dim InSourceText As XmlText =  XMLDom.CreateTextNode(exCheck.Source)
                    Dim InStackTraceText As XmlText =  XMLDom.CreateTextNode(exCheck.StackTrace)
 
                    InnerExceptionParentNode.AppendChild(InMessageNode)
                    InnerExceptionParentNode.AppendChild(InSourceNode)
                    InnerExceptionParentNode.AppendChild(InStackTraceNode)
 
                    InMessageNode.AppendChild(InMessageText)
                    InSourceNode.AppendChild(InSourceText)
                    InStackTraceNode.AppendChild(InStackTraceText)
 
                    InnerExceptionsNode.AppendChild(InnerExceptionParentNode)
                    parentNode.AppendChild(InnerExceptionsNode)
                    exCheck = exCheck.InnerException
                End While
            End If
 
            XMLDom.Save(strFilename)
 
        End Sub
 
        Public Shared  Sub WriteNewExToFile(ByVal ex As Exception, ByVal PageName As String)
 
            CreateExFile()
            Dim strFilename As String =  LogFileName
 
 
 
            Dim XMLDom As XmlDocument =  New XmlDocument()
            XMLDom.Load(strFilename)
 
            Dim parentNode As XmlElement =  XMLDom.CreateElement("ExceptionInfo")
            parentNode.SetAttribute("Exception_PageName", PageName)
            XMLDom.DocumentElement.PrependChild(parentNode)
 
            Dim DateTimeNode As XmlElement =  XMLDom.CreateElement("DateTime")
            Dim MessageNode As XmlElement =  XMLDom.CreateElement("Message")
            Dim SourceNode As XmlElement =  XMLDom.CreateElement("Source")
            Dim StackTraceNode As XmlElement =  XMLDom.CreateElement("StackTrace")
 
            Dim DateTimeText As XmlText =  XMLDom.CreateTextNode(DateTime.Now.ToString())
            Dim MessageText As XmlText =  XMLDom.CreateTextNode(ex.Message)
            Dim SourceText As XmlText =  XMLDom.CreateTextNode(ex.Source)
            Dim StackTraceText As XmlText =  XMLDom.CreateTextNode(ex.StackTrace)
 
            parentNode.AppendChild(DateTimeNode)
            parentNode.AppendChild(MessageNode)
            parentNode.AppendChild(SourceNode)
            parentNode.AppendChild(StackTraceNode)
 
            DateTimeNode.AppendChild(DateTimeText)
            MessageNode.AppendChild(MessageText)
            SourceNode.AppendChild(SourceText)
            StackTraceNode.AppendChild(StackTraceText)
 
            Dim exCheck As Exception =  ex
            Dim i As Integer =  0
 
            If Not exCheck.InnerException Is Nothing Then
                Dim InnerExceptionsNode As XmlElement =  XMLDom.CreateElement("InnerExceptions")
 
                While Not exCheck.InnerException Is Nothing
                    i = i + 1
                    Dim InnerExceptionParentNode As XmlElement =  XMLDom.CreateElement("InnerException" + i.ToString())
 
                    Dim InMessageNode As XmlElement =  XMLDom.CreateElement("InMessage")
                    Dim InSourceNode As XmlElement =  XMLDom.CreateElement("InSource")
                    Dim InStackTraceNode As XmlElement =  XMLDom.CreateElement("InStackTrace")
 
 
                    Dim InMessageText As XmlText =  XMLDom.CreateTextNode(exCheck.Message)
                    Dim InSourceText As XmlText =  XMLDom.CreateTextNode(exCheck.Source)
                    Dim InStackTraceText As XmlText =  XMLDom.CreateTextNode(exCheck.StackTrace)
 
                    InnerExceptionParentNode.AppendChild(InMessageNode)
                    InnerExceptionParentNode.AppendChild(InSourceNode)
                    InnerExceptionParentNode.AppendChild(InStackTraceNode)
 
                    InMessageNode.AppendChild(InMessageText)
                    InSourceNode.AppendChild(InSourceText)
                    InStackTraceNode.AppendChild(InStackTraceText)
 
                    InnerExceptionsNode.AppendChild(InnerExceptionParentNode)
                    parentNode.AppendChild(InnerExceptionsNode)
                    exCheck = exCheck.InnerException
                End While
            End If
 
            XMLDom.Save(strFilename)
 
        End Sub
 
        Private Shared  Sub CreateExFile()
 
            Dim strFilename As String =  LogFileName
            Dim AddDate As Boolean = (LogFileName.Contains("WFLogFile.xml"))
 
            If Not File.Exists(strFilename) Then
                If Not System.IO.Directory.Exists(LogPath) Then
                    System.IO.Directory.CreateDirectory(LogPath)
                End If
                If AddDate Then
                    Dim dt As DateTime =  DateTime.Now
                    strFilename = strFilename.Replace(".xml", dt.Day.ToString("00") + "-" + dt.Month.ToString("00") + "-" + dt.Year.ToString("0000") + ".xml")
                    LogFileName = strFilename
                End If
                Dim NewXmlFile As XmlTextWriter =  New XmlTextWriter(strFilename,System.Text.Encoding.Default)
 
 
                NewXmlFile.WriteStartDocument(True)
                NewXmlFile.WriteStartElement("ExceptionErrors")
                NewXmlFile.WriteEndElement()
                NewXmlFile.Flush()
                NewXmlFile.Close()
            End If
        End Sub
End Class

 

<--- Ends here --->