Event Logging and Distributed Logging in ASP.NET (1/6) | WebReference

Event Logging and Distributed Logging in ASP.NET (1/6)

current pageTo page 2To page 3To page 4To page 5To page 6
[next]

Event Logging in .NET

By Utpal Chakraborty ([email protected])

In an earlier article we had discussed how new tracing and debugging techniques in .NET make debugging easier. In this article we will view the event logging API that comes with .NET. We will also create a small library that can be used in most enterprise level applications that overcomes a few shortcomings of the standard .NET API for event logging.

Basic event logging

All the classes required for logging events to the windows event log are in the System.Diagnostics package. The most important class is the EventLog class. This allows reading and writing of event log entries. However, before any logs can be written an EventSource must be defined. Thus, in the simplest of scenarios, the following needs to be done

The source can also be defined (or created) during the writing itself. The API checks for the existence of the source and if none exists a source is created on the fly.

For reading event logs the EventLog class has a property called Entries which is a EventLogEntryCollection. As is obvious, this is a collection of EventLogEntry. Every entry in any event log is an instance of EventLogEntry. Along with some obvious properties, like Message and EventId it has an EntryType property. This is an enumeration which indicates the type of the event log. The different values of the Enumeration are Error, FailureAudit, Information, SuccessAudit and Warning. This allows for the grouping of log event entries into different levels of severity.

The following example code writes 10 event logs, reads them, and prints them out on the console. The program also deletes the log, although in real life this is never done since you would want the log around for reviewing purposes.

using System;
using System.Diagnostics;
namespace EventsTest
{
   class Test
   {
      [STAThread]
      static void Main(string[] args)
      {
         // write the events
         for(int i = 0; i < 10; i ++)
         {
            EventLog.WriteEntry("MyNewLog", 
                                "Message number " + i, 
                                EventLogEntryType.Information);
         }
         //read the events
         EventLog log = new EventLog("MyNewLog");
         foreach(EventLogEntry ei in log.Entries)
         {
            System.Console.WriteLine(ei.Message);
         }
         //delete the Log
         EventLog.Delete("MyNewLog");
      }
   }
}

One important thing to notice is that almost all the methods have an override that takes a machine name. This allows a program to write to the event log of a remote machine. This is useful when many applications are logging to a central server.

One interesting place where event logging is beneficial is the Application_Error method of an application in ASP.NET. A single line of event logging can greatly ease the tracking down of all exceptions that are not being specifically caught by the application code. The following line of code can be used:

EventLog.WriteEntry("MyLogname",Server.GetLastError().ToString(),EventLogEntryType.Error);

current pageTo page 2To page 3To page 4To page 5To page 6
[next]

Created: January 16, 2003
Revised: January 16, 2003

URL: https://webreference.com/programming/asp/logging/