Customizing Error Pages
page 3 of 3
by Steven Swafford
Average Rating: 
Views (Total / Last 10 Days): 20297/ 85

Writing Errors Out To The System Event Log

The System.Diagnostics namespace provides the foundation for troubleshooting your applications - debugging, event logging, performance monitoring, and interacting with system processes. Here we are concerned about the EventLog class and how we will use it.

For the purposes of this example, we are just going to log any application errors. We are going to be logging these type of entries to the Application Log on the server itself. This is where most applications log their errors, warnings and information.

Example Class:

using System;
using System.Diagnostics;
using System.IO;

namespace System.Diagnostics
 /// <summary>
 /// Summary description for article06012004.
 /// </summary>
 public class article06012004

  static void Main(string[] args)

  public static void MoveDirectory()
   string directoryPathToMove = @"c:\MyDirectory";
   string directoryPathMoveLocation = @"c:\MyDirectory1";

    Directory.Move(directoryPathToMove, directoryPathMoveLocation);
   catch(Exception err)


  public static void logError(string strError, string strSource) 
   System.Diagnostics.EventLog myEventLog = new System.Diagnostics.EventLog();
   myEventLog.Source = strSource;
   myEventLog.WriteEntry (strError, System.Diagnostics.EventLogEntryType.Error);

If an Exception is in fact thrown you can then see this error in your event log.

EventLog Source

By using this approach you can code for as much or as little details as your requirements ask for. This way you will have the necessary information to fix any problems that may arise.

View Entire Article

User Comments

Title: Very good info, thanks   
Name: Joe
Date: 2004-06-14 4:49:41 AM
This was very helpful for me.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-29 11:58:12 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search