|
Write To Vista Event Log Using C#
Event Log is a central place to log application events. These events can be errors, warnings or just information. Each event log entry in Windows Vista has a level of event, date and time the event occurred, source of event, an event Id and a task category. While event logs such as Application, System, Security exist by default, you can also create your own event log.

You can also write to event log from your own application using System.Diagnostics.EventLog class. The best practice is to check if the source exists. If it does not then you should create it before you write to it.
string source = "EventLog Demo";
if (!EventLog.Exists(source))
EventLog.CreateEventSource(source, "Application");
We can now safely write to event log with a statement like this.
EventLog.WriteEntry(source, "Writing Error", EventLogEntryType.Error, 7);
In the above statement I am writing an event of type error. There are five EventLogEntryType values available. Event viewer will show the appropriate icon depending on the entry type.

After executing the code we can verify that my event entry is viewable in Event Viewer.

Writing application events into Event Log should be a standard message/error logging practice for any application. Windows provides this standard location where it records its own events and this is where you look first when troubleshooting. Thus it makes perfect sense to use this for your own custom applications.
Get Updates By Email
Popular Post
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Free Icons And Images With Visual Studio 2008
- Windows 7 Control Panel In Classic Mode
- Dynamic Sort With LINQ
- Use SqlConnection With LINQ To SQL
- StyleCop Tutorial
- Write To Vista Event Log Using C#
- More Details Emerge On Microsoft Master Certification
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL


September 5th, 2008 at 3:25 am
Does this work correctly for you in Vista? I get a security exception when I try to create event log sources under Vista.
September 6th, 2008 at 8:27 pm
Chad,
I wrote a simple console application to re-test what I posted. And yes it works fine on my Vista machine. Here is the code:
static void Main(string[] args)
{
string source = “Test Log”;
if (!EventLog.Exists(source))
EventLog.CreateEventSource(source, “Application”);
EventLog.WriteEntry(source, “Writing Error”, EventLogEntryType.Error, 7);
}
Can you post the exception you are getting.
September 18th, 2008 at 11:34 pm
This code does work for Vista with disabled UAC. Otherwise, a security exception is thrown. I’m looking for a solution for this behavior, but without luck yet. If I do find somthing that works, I will tell you.
September 19th, 2008 at 6:04 pm
Matti,
That certainly will be an issue. In normal course you will create performance counters when your application is installed and the installer will need to run as administrator.
September 19th, 2008 at 7:12 pm
Deepak,
in normal course, this would be the right way. But we deploy our application via ClickOnce deployment and there you don’t have any way to create such things during the installation. So my solution will be, to write a helper application which creates the EventLog-source which needs to be run as administrator.
September 22nd, 2008 at 11:57 pm
Yeah, it’s the UAC issue… I’m able to get around it by disabling UAC, but it’s annoying.
November 7th, 2008 at 8:20 am
does not your second line need to be if (!EventLog.SourceExists(source)) ?
November 27th, 2008 at 9:55 pm
Well, running as admin will also work, of course.
Or as SYSTEM (while installing an MSI, for example).
We need a system service anyway, so I just made it do different things such like this.
January 11th, 2009 at 7:58 pm
How to use kind of different of “Application” log.
I trying to write to custom log use custom source.
December 1st, 2009 at 2:56 am
Bookmarked your feed! Thanks!