Powered by Blogger.

Saturday, February 22, 2014

Write exception to Event Log in Asp.Net C#.Net



In this article we will discuss how to write exception to Event Log in Asp.Net.  You can check my previous posts on:

- Get stored procedure source text in sql server 2008

- Working with enterprise library for data access in asp.net

- Triggers in sql server 2008

Event log will be accessible from Start -> Run -> eventvwr. To write the exception message in the Event log is now easy in .Net by use of the namespace System.Diagnostics;

Below is the code to write to event log:
using System.Diagnostics;

try
{
// Code
}
catch (System.Exception ex)
{

EventLog log = new EventLog();
log.Source = "Comes from .Net Application";
log.WriteEntry(ex.Message, EventLogEntryType.Error);

}
finally
{
// Clean up block.
}

Here in the above code the exception message will be written in the event log with source as "Comes from .Net Application".

Now open the event viewer window ( Start -> Run -> eventvwr ) and Navigate to Windows Logs -> Application , You will able to see the error message.