Click here to Skip to main content
13,348,874 members (77,362 online)
Click here to Skip to main content
Add your own
alternative version


81 bookmarked
Posted 22 Aug 2009

How To Create a Windows Event Log and Write your Custom Message as well

, 22 Aug 2009
Rate this:
Please Sign up or sign in to vote.
A simple class to create a Windows event log and write your custom message as well
Fig-1 (Event viewer - Showing the new log with sample information)


This article will give you an idea about how to create a Windows event log and write your custom message into the event log.


Most of the developers are very much familiar with the Windows event log API. When developers create a Windows based application, it may be an ActiveX component, any customized DLL library, a service application, etc.; it is a very common practice to write several types of information into the event log, which is generated by that particular application runtime so we can easily keep track of all the information.

Using the Code

Before we start understanding the code, we need a little concept on System.Diagnostics namespace. We will use the namespace to manage an event log. The namespace System.Diagnostics provides a class name as “EventLog” to create and write a message into the eventlog.

Let’s take an example, our goal is to write a simple C# .NET class which will create a new log name as “myEventLog” and write a message “I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.” as an information.

To achieve this, we need to use the following methods, properties and enum which will be found in the System.Diagnostics namespace.


  • SourceExists: Determines whether an event source is registered on the local computer or not.
  • CreateEventSource: Establishes an application as able to write event information to a particular log on the system.
    More details can be found at this link.
  • WriteEntry: Writes an entry in the event log, i.e., writes an information type entry, with the given message text, to the event log.
    More details can be found at this link.


  • Source: Gets or sets the source name to register and use when writing to the event log.
  • Log: Gets or sets the name of the log to read from or write to.


  • EventLogEntryType: Specifies the event type of an event log entry.
    More details can be found at this link.

Sample Code Example

public class ClsEventLog
      public bool CreateLog(string strLogName)
          bool Result = false;

                  System.Diagnostics.EventLog.CreateEventSource(strLogName, strLogName);
                  System.Diagnostics.EventLog SQLEventLog =
              new System.Diagnostics.EventLog();

                  SQLEventLog.Source = strLogName;
                  SQLEventLog.Log = strLogName;

                  SQLEventLog.Source = strLogName;
                  SQLEventLog.WriteEntry("The " + strLogName + " was successfully
          initialize component.", EventLogEntryType.Information);

                  Result = true;
              Result = false;

          return Result;
      public void WriteToEventLog(string strLogName
                                , string strSource
                                , string strErrDetail)
          System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();

              if (!System.Diagnostics.EventLog.SourceExists(strLogName))

              SQLEventLog.Source = strLogName;
                                    + Convert.ToString(strErrDetail),

          catch (Exception ex)
              SQLEventLog.Source = strLogName;
              SQLEventLog.WriteEntry(Convert.ToString("INFORMATION: ")
                                    + Convert.ToString(ex.Message),
              SQLEventLog = null;


I hope that this article might be helpful to you. Enjoy!


  • 22nd August 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Md. Marufuzzaman
Bangladesh Bangladesh
A well experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by and also selected as a mentor of Published over 100 technical articles in various software development resource sites (i.e.,, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.

Over twelve years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.I have also more than two years’ of strong experience in mobile-VAS (platform development).

An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint.Honest, believes in transparency, commitment and teamwork.

Expertise: Software/Solution Architect, Research, MIS, Data Analytics, Data Mining, BI, SaaS platform base application development, Large scale Win32 Form/Web based business software solutions, Security, Enterprise applications development, integration, etc.

Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12/14 , Oracle version 10/11, MySql version 5.1, 5.5, 5.7, Visual C#, VB.NET, ASP.NET, API, MVC, WebAPI , WPF, WCF, PHP, Microsoft Power BI, SPSS, PS2, R, Add-In, Visual Basic etc.

.Net UI component: Telerik, DevExpress, Ext.Net.
Scripting language: JavaScript, AngularJS, node.JS.
Source control / Subversion: Git,SVN.
Development methodologies: Agile,RAD.
Project Management / Issues Tracking Tools: JIRA, ClockingIt.

You may also be interested in...


Comments and Discussions

GeneralWindows Event Log Custom Message Pin
GauravKM8-Dec-17 3:28
professionalGauravKM8-Dec-17 3:28 
GeneralMy vote of 5 Pin
deatharthas3-Dec-12 16:47
memberdeatharthas3-Dec-12 16:47 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman4-Dec-12 5:57
mentorMd. Marufuzzaman4-Dec-12 5:57 
GeneralMy vote of 5 Pin
Joe Sam27-Nov-12 20:47
memberJoe Sam27-Nov-12 20:47 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman28-Nov-12 8:49
mentorMd. Marufuzzaman28-Nov-12 8:49 
GeneralMy vote of 5 Pin
JF20155-Jun-12 21:48
memberJF20155-Jun-12 21:48 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman28-Nov-12 8:47
mentorMd. Marufuzzaman28-Nov-12 8:47 
GeneralMy vote of 5 Pin
Prasanta_Prince14-Jul-11 20:12
memberPrasanta_Prince14-Jul-11 20:12 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman6-Oct-11 0:13
mvpMd. Marufuzzaman6-Oct-11 0:13 
GeneralGreat Pin
shahadatbjri25-Aug-09 9:54
membershahadatbjri25-Aug-09 9:54 
GeneralRe: Great Pin
Md. Marufuzzaman23-May-10 23:09
mvpMd. Marufuzzaman23-May-10 23:09 
Thanks......... Smile | :)
Md. Marufuzzaman

I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

GeneralPermissions Pin
Grand Poobah 7722-Aug-09 8:54
memberGrand Poobah 7722-Aug-09 8:54 
GeneralRe: Permissions Pin
Md. Marufuzzaman22-Aug-09 11:17
groupMd. Marufuzzaman22-Aug-09 11:17 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 22 Aug 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid