Creating an Error Logging Class
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Creating an Error Logging Class
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Algorithm Fun [next Lesson]  Stored Procedures
Creating an Error Logging Class
  by: tiberius

Creating an Error Logging Class

by: tiberius

When I worked in VB6, you’d have to ‘roll-your-own’ error logs. It wasn’t that much of a hassle, but you’d have to let the server administrator know where the log was, and you’d have to check it every so often to make sure that your application wasn’t erring out. With .NET, Microsoft has provided some really great classes to help track problems with your application.

System.Diagnostics

The System.Diagnostics namespace provides the backbone for troubleshooting your applications – debugging, event logging, performance monitoring and interacting with system processes. We can use some of the classes found in the System.Diagnostics namespace to help us be a little more productive in determining what caused our application to error out.

In this case, we’re concerned about the EventLog class and how we’ll use it to report out what happened to our application.

Event Log Class

The EventLog class allows us access to event logs, which are a running list of events that occurred on a machine. These events can be errors or warnings or just information about an application or process. When we use the EventLog class to connect to a machine’s event log, we can write entries, delete entries or list out what’s contained in that log.

Writing to the Log

Because we’ll be using the EventLog class to write errors that our application raised, we’ll have to provide the name of our application, and some information about the error.

Before our application does the actual work of logging the error, we have to provide an event Source. The event Source essentially tells the EventLog which application has erred out, so developers and administrators have an idea of which application the error originated from.

In addition to telling the EventLog class where the error came from, we also need to provide it with a description of what the error was – this is called the entry message.

I usually pass the entire error message from my application into this parameter, but you could trim it down if you wanted a brief description of the error or provide your own custom error. We’ll also be passing one of five EventLogEntryTypes, which are an enumeration of the different types of events that can be logged:

  • Error
  • Failure Audit
  • Information
  • Success Audit
  • Warning
For the purposes of our Logger Class, we’re just logging errors so we’ll be using the System.Diagnostics.EventLogEntryType.Error enumeration.

If the need arose that you needed to provide additional information beyond these two parameters, you can provide an application-defined eventID and category which are displayed in their respective columns in the event viewer. You can also tack on a binary attachment to the entry if need be - I can imagine that you might want to provide a homespun XML document that went more into detail that what is exposed in the event log.

In our case, we’re really only concerned with providing the Source, the EventLogEntryType and the Entry Message to the EventLog class.

Although we could create our own custom event log, we’re going to be logging our entries to the Application Log. This is where most applications log their errors, warnings and information.

Logger Class

When I’m writing my catch statement in my code, I make a reference to my Error Logging class and dump the content of the error object to the Event Log. This way I always have a log of what error was raised in my application. After I log the error, I can let the rest of the code in my catch statement do some more work.

When an error is caught in our application, we’ll need make reference to our Logger class and pass in the necessary parameters to our logError method.
public void logError(string strError, string strSource) 
{
We declare a reference to the System.Diagnostics.Eventlog object:
System.Diagnostics.EventLog oEV = new System.Diagnostics.EventLog();
It’s very important to set a source to the Eventlog!
oEV.Source = strSource;
Here we are writing the error to the EventLog, plus we are letting it know what type of Event Log Entry Type we are writing – this is usually the severity of the event: Error, Failure Audit, Information, Success Audit and Warning. In our case, we’re just logging the Errors that our application or process has risen.
oEV.WriteEntry (strError, System.Diagnostics.EventLogEntryType.Error);
Always remember to close any object that writes to a file!
oEV.Close(); 
}
Ok, that wasn’t too painful of a walk-through, was it?

Using the Logger Class

Now that we’ve created our Logger class, we’re going to use it in all of our catch statements. This will ensure that we log every error that was raised by our applications. The implementation of the Logger class is simple – you merely instantiate the class within the catch statement, convert the System.Exception object to a string, and pass in the name of your application.
catch(System.Exception err)
{
    Logger oLogger = new Logger();
    oLogger.logError(err.ToString(),"TestApplication");
}

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Creating an Error Logging Class
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Algorithm Fun [next Lesson]  Stored Procedures


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
carlos_roc.. 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center