Exception Handling (Exception logging)

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ExceptionLogger
{
public class ExceptionLogger
{
public static void WritetoCustomEventLog(Exception paramEntry, EventLogEntryType paramType)
{
string str_Source = "ERROR LOG"; //Source Name
string str_LogType = "My Test LOG"; //Your log Name if it does not exists then first it will create and then add entry
string str_Machine = "."; //machine name

if (!System.Diagnostics.EventLog.SourceExists(str_Source, str_Machine))
{
System.Diagnostics.EventLog.CreateEventSource(str_Source, str_LogType, str_Machine);
}
string ExceptionDetail = "";
ExceptionDetail = "Message : " + paramEntry.Message + "\n";
ExceptionDetail += "Stack Trace : " + paramEntry.StackTrace + "\n";
ExceptionDetail += "Source : " + paramEntry.Source + "\n";
ExceptionDetail += "Target Site:" + paramEntry.TargetSite + "\n";
ExceptionDetail += "Inner Exception:" + paramEntry.InnerException + "\n";
ExceptionDetail += "Help Link:" + paramEntry.HelpLink + "\n";
EventLog CustomLog = new EventLog(str_LogType, str_Machine, str_Source);

CustomLog.WriteEntry(ExceptionDetail, paramType);

}
}
}

Comments

Popular posts from this blog

Use configSource attribute to manage Web.Config sections in ASP.Net 2.0

DevOps - Key Concepts - Roles & Responsibilities - Tools & Technologies