Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Logging exceptions in your event log

I occasionally get asked for this, so I’m going to post it here for
people to find.  This is how to put an exception from a .Net
application into a custom log in the event viewer.

Let’s say you catch in exception in your code and you want to put it in the event logs.  It’s a piece of cake.

VB.Net code to log an exception into the event logs

Public Shared Sub LogException(ByVal ErrorDescription As String)

        ‘ The name of our log in the event logs

        Dim Log As String = “AspNetError”

 

        ‘ Check to see if the log for AspNetError exists on the machine

            If not, create it

        If (Not EventLog.SourceExists(Log)) Then

            EventLog.CreateEventSource(Log, Log)

        End If

 

        ‘ Now insert your exception information into the AspNetError event log

        Dim logEntry As New EventLog

        logEntry.Source = Log

        logEntry.WriteEntry(ErrorDescription, EventLogEntryType.Error)

 

    End Sub

 Here is the C# code to do the same thing:

C# code to log an exception into the event logs

public static void LogException(string ErrorDescription)

{

            // The name of our log in the event logs

            string Log = “AspNetError”;

 

            // Check to see fi the log for AspNetError exists on the machine

            //          If not, create it

            if ((!(EventLog.SourceExists(Log))))

            {

                       
EventLog.CreateEventSource(Log, Log);

            }

 

            // Now insert your exception information into the AspNetError event log

            EventLog logEntry = new EventLog();

            logEntry.Source = Log;

            logEntry.WriteEntry(ErrorDescription, EventLogEntryType.Error);

}

To test this, just creating the following code in a method somewhere:

Test code

try

{

            ThrowException();

}

catch (Exception exp)

{

            LogException(exp.ToString());

            throw;  // or whatever you want to do with it

}

 Where ThrowException does nothing but throws an exception

ThrowException

private void ThrowException()

{

            throw new Exception(“Test”);

}

 And you get a nice little entry into your event viewer (I ran it twice, that is why you see 2 entries):

Event Log

This entry was posted in .Net Development. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

5 Responses to Logging exceptions in your event log

  1. Josh and Dave are both correct in that you cannot create the log source at runtime without admin privs. If you are running a windows app and are a member of the admin group, you won’t have a problem. If you have changed your processes model username from MACHINE to SYSTEM, you won’t have a problem with Asp.Net applications. But under most circumstances, follow their advice and either create the event source before hand, or handle it at runtime.

  2. Dave says:

    I agree. You need ADMIN privileges to create an eventlog source. You’ll only have those at Install time, not runtime.

  3. How would giving the assembly full trust make a difference?

    I would suggest that any event source creation be done at “install” time, (using a class derived from Installer, and the InstallUtil.exe or a setup package), rather than at runtime.

  4. Wasn’t thinking when I named my event log AspNetError. Didn’t intend this to be asp specific, but the answer is to give the assembly full trust or use impersonation. The AspNet account, by default, does not have access to the event logs.

  5. sahilmalik says:

    Hey dude,

    What about security errors you might get from aspnet when using the above?

    – SM

Leave a Reply