The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Logging exceptions in your event log

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Raymond Lewallen

Posts: 312
Nickname: rlewallen
Registered: Apr, 2005

Raymond Lewallen is a .Net developer and Sql Server DBA
Logging exceptions in your event log Posted: Jul 13, 2005 1:14 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Raymond Lewallen.
Original Post: Logging exceptions in your event log
Feed Title: Raymond Lewallen
Feed URL: /error.htm?aspxerrorpath=/blogs/raymond.lewallen/rss.aspx
Feed Description: Patterns and Practices, OOP, .Net and Sql
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Raymond Lewallen
Latest Posts From Raymond Lewallen

Advertisement

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

Read: Logging exceptions in your event log

Topic: Zanebug Article Published in .NET Developer's Journal Previous Topic   Next Topic Topic: Rails vs. J2EE

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use