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.
12 Responses to Write To Vista Event Log Using C#
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML


Does this work correctly for you in Vista? I get a security exception when I try to create event log sources under Vista.
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.
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.
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.
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.
Yeah, it’s the UAC issue… I’m able to get around it by disabling UAC, but it’s annoying.
does not your second line need to be if (!EventLog.SourceExists(source)) ?
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.
How to use kind of different of “Application” log.
I trying to write to custom log use custom source.
Bookmarked your feed! Thanks!
I believe you can add a statement to the manifest file you elevate your process to system admin rights. But it may have to be a signed program. Not sure.
does this program work in windows 7. It shows security exception for me.