Click here to Skip to main content
Licence 
First Posted 12 Jun 2007
Views 16,539
Downloads 152
Bookmarked 6 times

How to write custom log and source for custom log in C#

By | 12 Jun 2007 | Article
It will show you the way for writing custom log in C#.Net

Introduction

This article will introduce you how to write custom log event for your tool/program. If you are going to create a tool then most of the professional like to use event log for logging its information, error etc and user can view these log in event log viewer.

Background:

Whenever you are going to create custom event log it creates registry entry in HKLM/ SYSTEM/ Services/ Eventlog.

For example if you are going to create custom log "MyLog" then the registry entry for MyLog will be HKLM/ SYSTEM/ Services/ Eventlog/MyLog and event file will be stored in %SystemRoot%\System32\config\Biopassw.evt path.

If you want to change the path for event log file then you have to change the path in File key of event log.

Using the code

Here the code :)

 
//Class for create custom log
using System.Diagnostics;
namespace EventExp
{
    class MyLog
    {
        //
        //create string object for source,log and event.
        //
        string mySource;
        string myLog;
        string myEvent;
        public MyLog()
        {
            //
            //assign text to source,log and event string.
            //
            mySource = "Sample Application";
            myLog = "MyLog";
            myEvent = "Sample Event";
        }
        public void CreateMyLog()
        {
            //
            //Check the event log if not exist then create it.
            //
            if (!EventLog.SourceExists(mySource))
            {
                EventLog.CreateEventSource(mySource, myLog);
            }
        }
        public void WriteMyLogEntry()
        {
            //
            //Will write entry in MyLog vent log.
            //
            EventLog.WriteEntry(mySource, myEvent);
            EventLog.WriteEntry(mySource, myEvent,
                EventLogEntryType.Warning, 234);
        }
    }
}
In program.cs inside main function:
//
            //Now write custom log
            //
            MyLog objectMylog = new MyLog();
            objectMylog.CreateMyLog();
            objectMylog.WriteMyLogEntry();


In event viewer we can see the log "MyLog" having two event log entry for Mylog written by WriteMyLogEntry function.

Points of Interest

When you creates source for log it writes mapping in side registry. Once you created source and if you want to change that source for other system or custom log then after creating the source with new custom/system event log you must have to restart the system.

License

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

Neeraj Kumar Suryawanshi

Web Developer

India India

Member

Born in Chhindwara MP in India. Right now in Bangalore the IT hub of world.
Currently working in security domain using .Net framework 2.0. Having 3+ years or exepriance in .Net technology.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNot very user friendly PinmvpMark Nischalke1:49 13 Jun '07  
GeneralRe: Not very user friendly PinmemberNeeraj Kumar Suryawanshi6:54 20 Jun '07  
GeneralRe: Not very user friendly PinmvpMark Nischalke7:33 20 Jun '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 13 Jun 2007
Article Copyright 2007 by Neeraj Kumar Suryawanshi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid