Fig-1 (Event viewer - Showing the new log with sample information)
Introduction
This article will give you an idea about how to create a Windows event log and write your custom message into the event log.
Background
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.
Methods
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.
Properties
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.
Enum
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;
try
{
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;
}
catch
{
Result = false;
}
return Result;
}
public void WriteToEventLog(string strLogName
, string strSource
, string strErrDetail)
{
System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();
try
{
if (!System.Diagnostics.EventLog.SourceExists(strLogName))
this.CreateLog(strLogName);
SQLEventLog.Source = strLogName;
SQLEventLog.WriteEntry(Convert.ToString(strSource)
+ Convert.ToString(strErrDetail),
EventLogEntryType.Information);
}
catch (Exception ex)
{
SQLEventLog.Source = strLogName;
SQLEventLog.WriteEntry(Convert.ToString("INFORMATION: ")
+ Convert.ToString(ex.Message),
EventLogEntryType.Information);
}
finally
{
SQLEventLog.Dispose();
SQLEventLog = null;
}
}
}
Conclusion
I hope that this article might be helpful to you. Enjoy!
History
- 22nd August 2009: Initial post
He is the founder & CEO of MNH Technologies and working for urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc. He has over ten years of professional experiences in design and developing Client-Server, Multi-Tier, Database, Web based business software solutions, Enterprise Applications, API, WebAPI, Google Analytics implementation, Add-In, Documentation & Technical Writing etc for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, C#, VB.NET, ASP.NET, PHP, RoR, Visual Basic etc. He has also more than two years experience in Mobile-VAS (Platform Development).
He worked for various software development & technology consulting. His core focus on technologies to create dynamic data-driven systems that add value to your business and dynamic technology consulting that builds advanced solutions for the industries across the various vertices.
He also work as a Solution Architect at Dhrupadi Techno Consortium Limited (DTCL) and responsible for analyzing business requirements and offered optimum solutions (multiple options), which would address all current requirements, provide flexibility for future growth and allow smooth transition between old system and new system.
He graduated with honors from The University of Asia Pacific, in Computer Science and Engineering. He was awarded as “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a Mentor of CodeProject.com
Specialties: Software Development Management, System Integration, Data Warehouse Architecture, Virtualization.