Logging exceptions to the windows eventviewer Part 73
Text version of the video
Slides
All ASP .NET Text Articles
All ASP .NET Slides
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
In this video we will discuss about logging exceptions to windows eventviewer. In the previous videos we discussed about, creating the custom event log and event source in windows event viewer. Please watch Windows Event Viewer – Part 72, before continuing with this session.
Simplified Log() Method using do-while loop. The complete code for this video can be found at the following URL
public static void Log(Exception exception)
{
StringBuilder sbExceptionMessage = new StringBuilder();
do
{
sbExceptionMessage.Append(“Exception Type” + Environment.NewLine);
sbExceptionMessage.Append(exception.GetType().Name);
sbExceptionMessage.Append(Environment.NewLine + Environment.NewLine);
sbExceptionMessage.Append(“Message” + Environment.NewLine);
sbExceptionMessage.Append(exception.Message + Environment.NewLine + Environment.NewLine);
sbExceptionMessage.Append(“Stack Trace” + Environment.NewLine);
sbExceptionMessage.Append(exception.StackTrace + Environment.NewLine + Environment.NewLine);
exception = exception.InnerException;
}
while (exception != null);
if (EventLog.SourceExists(“Hello”))
{
EventLog log = new EventLog(“Hello”);
log.Source = “Hello”;
log.WriteEntry(sbExceptionMessage.ToString(), EventLogEntryType.Error);
}
}