Click here to Skip to main content
12,548,514 members (47,188 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

A Simple Customizable Windows Event Logger Application

, 20 Feb 2005
Rate this:
Please Sign up or sign in to vote.
An article on writing a simple Windows Event Logger.


Effective logging in a production environment whether it is a Client-Server application, backend application (such as windows Services), or e-commerce web site, is becoming a common business requirement. Event logging is the heart of any application for debugging, tracing and monitoring the application. Once any application goes into production environment, Event Log is the most effective and the only way to monitor the application and to find out the application failure reasons. It is the fastest way of getting the application failure details only if the logging mechanism is effective and is configurable to suit the business needs. This article is the attempt to provide a simple, configurable and effective logging mechanism to application designers.

In this article, I'll be discussing:

  1. Most common requirements to write any logger application.
  2. How to write a simple logging application which uses Windows Event log to log error, warning and information messages.
  3. Flexibility of the logger application to suit client needs.

The logger application code is downloadable. It uses SQL Server DB to store the Info, Error and Warning Messages. This is to avoid hard-coding of messages in the code which gives flexibility of updating any message without the need to recompile the code.

This application is configurable and protects the client's interest of logging only particular type of messages. It also provides the mechanism to hardcode the message and message type and which will be logged into the Windows Event Log.


I've seen designers and developers struggling for coming up with a proper logging mechanism which gives a proper picture of the application and also is intelligent enough to filter particular type of messages. Here, I'll discuss how to write a logger application in which you can set a log level to filter particular type of messages. This allows clients to filter unwanted messages and protects from being logged into the event log.

Most common requirements to write a logger application

During my experience, I've observed that below are the most common requirements for writing any logger application:

  1. It should be easy to log the message into log sink without any need of specifying the type of message.
  2. It should be flexible enough to change the message description without any need to recompile the code.
  3. It should be possible to hard-code the debug message (useful specifically in case of backend applications like Windows services, which helps for debugging).
  4. It should have an user friendly interface to sort the messages depending upon type, user, date, source etc.

Easy steps to log the message into Windows Event Log

The easy and simple steps to test the event logger application is just download the code, execute the DB script, and run the code as it is. That's it! The code contains the logger DLL and also the test application to test that DLL. The detailed description is as below:

Step 1: Create the Logging Database

Download and execute the DB script in your application's SQL Server DB. This will create a table (name: Messages) for storing the predefined messages. The script also will create the stored procedures required to retrieve the corresponding message and to log into the event log.

Step 2: Configure the application settings

  1. In you application configuration file, specify the connection string and MessageLoggingParameter.
  2. MessageLoggingParameter value can be set from 0 to 7, as below:
    • MessageLoggingParameter 0:-No messages will be logged.
    • MessageLoggingParameter 1:-Only information messages will be logged.
    • MessageLoggingParameter 2:-Only warning messages will be logged.
    • MessageLoggingParameter 3:-Only error messages will be logged.
    • MessageLoggingParameter 4:-Only information and warning messages will be logged.
    • MessageLoggingParameter 5:-Only warning and error messages will be logged.
    • MessageLoggingParameter 6:-Only information and error messages will be logged.
    • MessageLoggingParameter 7:-All messages will be logged.
  3. Specify the application Log Name. This will be created as a separate entry on the left side pane of the Windows Event Log.
    <add key = "LogName" value="Test Log"/>
  4. Specify the application source:
    <add key = "EventSource" value="My Application"/>

Step 3: Log the message:

Now you are ready to log the message into Windows event log. This is an easy step. Just add a few messages with MessageID, type of message (Error, Information, Warning), and message description, into the Messages table. This creates your message store, which will help you to update any message without the need of recompiling the code, and this will be logged into Windows Event Log. The code to log the message into event log is as below:

 //Instantiate the Logger class
Logger logger = new Logger();
//Call the Write method and pass the MessageID. 
//This retrieves the corresponding info from DB 
//and log it to Windows Event Log.

//Alternatively, if you don't want to retrieve 
//the message from DB and want to write your 
//own message, call the overloaded Write 
//method with 2 parameters.
logger.Write("This is my message","Information");

Logger class:

Let's see what we are doing in the logger class which is taking care of configuration, message retrieval from DB and logging messages into Windows Event Log.

The Write method first retrieves the message type and message description from the DB depending upon the MessageID passed to it. The Write method then creates the Event Log and Event Source if it doesn't exist.

EventLog objEventLog = new EventLog(strLogName);
    EventLog.CreateEventSource(strEventSource, strLogName );

Then the function gets the information about the type of message and client's interest configured in the app.config file whether to log the message or not, and logs it accordingly.

//If message is a Information and MessageLoggingParameter
// is configured to log this message then log it
if( (strMessageType.ToUpper().CompareTo("INFORMATION")
   == 0) && ( (iMsgLogParam == 1) || (iMsgLogParam == 4)
   || (iMsgLogParam == 6) || (iMsgLogParam == 7)
   || (iMsgLogParam == 8) ) )

However, the steps of taking message details from DB is skipped in the overloaded method of Write method as the message description and message type is passed by the user.


I really hope the Logger application design and sample code here will be useful for application designers and developers.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Pankaj A. Chitriv
Web Developer
India India
He likes traveling a lot, especially long drives. He occasionally plays guitar, fascinated to watch WWE and likes spending time with his family and friends.

About the work, Pankaj is playing with the MS technologies since last couple of years. He designed and architected quiet a few applications and products which specially includes load balancing, DB clustering and also known to various architectural patterns and application blocks.

You may also be interested in...


Comments and Discussions

QuestionHow to log explorer events Pin
rjknis28-May-07 23:28
memberrjknis28-May-07 23:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 21 Feb 2005
Article Copyright 2005 by Pankaj A. Chitriv
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid