.NET Tip: Logging Exceptions to the Event Log

Assuming you have Remote Desktop or some other access to a server, the system event log is a good place to store information about your application. I personally prefer to put some information into the database, but errors such as the inability to make a database connection obviously would preclude you from storing an error in the database.

The code to write entries to the event log is fairly straightforward and can easily be added to a base page class or other utility class for ease of use. To configure how the event log writer works, you also can use the configuration file and either a custom configuration section or the standard AppSettings area.

The following code creates three public functions and a private one to do the real work. You easily can read the code to determine which type of entry is being created:

public void LogInformation(string message)
{
   LogEntry(message, EventLogEntryType.Information);
}

public void LogWarning(string message)
{
   LogEntry(message, EventLogEntryType.Warning);
}

public void LogError(string message)
{
   LogEntry(message, EventLogEntryType.Error);
}

private void LogEntry(string message, EventLogEntryType logType)
{
   if (!EventLog.SourceExists("NCS.Web"))
      EventLog.CreateEventSource("NCS.Web", "Application");

   EventLog ncsLog = new EventLog();
   ncsLog.Source = "NCS.Web";
   ncsLog.WriteEntry(message, logType);

}

Each call to LogEntry passes a EventLogEntryType constant, which can be one of the three shown or SuccessAudit or FailureAudit. Inside the LogEntry routine, the code first determines whether an event source called NCS.Web (the internal name for the application) has been created. If it hasn't, it creates the event source with a name and location where the log should be stored—whether it should go to the Application, Security, or System log, or a custom log you can create. In this case, the log entries are placed into the Application log, and then an EventLog object is created, the source is set, and the message is written out. You also can specify an event ID and/or a category for the log entry. This makes it easier to sort through the logs when you are diagnosing a problem.

To make this routine more useful, you also could write the events to a database table or to a flat file because it can be somewhat difficult to view the logs from a Web site.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds