How to log exceptions under event viewer

Topics: Logging Application Block
Jun 2, 2010 at 3:45 PM

Hi There,

I've tried logging any exception inheriting from the general .NET Exception Class to the event viewer and it was OK.

My problem is now that it's logged by default under the Application node in the event viewer. Actually I wanna change the log destination from Application node to another new node having my project name.

I use the following code to log my exceptions:

Exception exThrow;
ExceptionPolicy.HandleException(Page.Server.GetLastError(), "General Policy", out exThrow);

I don't either know if my code can create that special node, entitled with my project name, programmatically or should I create it in advance and refer to it from my web.config properties and if so, which one would be the candidate to have my name instead of Application?

 

Thanks in advance

Jun 3, 2010 at 2:46 AM
Edited Jun 3, 2010 at 2:47 AM

Hi,

First thing you need to do is to configure your exception policy to use a Logging Handler. You can do this by           

                1. Add the logging handler as the first handler for each exception type.

                2. Configure the logging handler: 

  •  
    •  
      • Enter the event ID.
      • Enter the logging categories.
      • Select the severity.
      • Enter the title of the log entry.
      • Select the formatter type name.
      • Enter the priority.

(you can check the documentation for more details http://msdn.microsoft.com/en-us/library/ff664711(v=PandP.50).aspx)

Then, configure your Logging Application Block specifically the Event Log TraceListener that will be used by your exception policy to log the exception in the EventLog.

When configuring the Event Log trace listener, you can use an arbitrary event source name (configure Event Log TraceListener Log Name and Source Name Property). However, event sources must be registered with the operating system before you can successfully log using them. Normally the .NET framework will automatically create event sources the first time you use them, but creating event sources require administrator privileges. As such, any applications without admin rights (like almost all web applications) cannot create the event source and logging fails. (http://entlib.codeplex.com/wikipage?title=Why%20are%20messages%20not%20logged%3f&referringTitle=EntLib%20FAQ)

When deploying your application, you should include an installer class to create your event sources. You can check using the EventLogInstaller Class on how to create your own installer.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com