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

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)
  {
   MoveDirectory();
  }


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


   try
   {
    Directory.Move(directoryPathToMove, directoryPathMoveLocation);
   }
   catch(Exception err)
   {
    logError(err.ToString(),"ASPAllianceArticle06012004");
   }


  }


  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);
   myEventLog.Close(); 
  }
 }
 
}

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
Comment:
This was very helpful for me.






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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 8:18:33 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search