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.

image

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.

image

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

image

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.

Technorati Tags: ,,

Tagged with:
 

12 Responses to Write To Vista Event Log Using C#

  1. chad says:

    Does this work correctly for you in Vista? I get a security exception when I try to create event log sources under Vista.

  2. Deepak says:

    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.

  3. Matti says:

    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.

  4. Deepak says:

    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.

  5. Matti says:

    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.

  6. chad says:

    Yeah, it’s the UAC issue… I’m able to get around it by disabling UAC, but it’s annoying.

  7. philip says:

    does not your second line need to be if (!EventLog.SourceExists(source)) ?

  8. 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.

  9. Barchen says:

    How to use kind of different of “Application” log.
    I trying to write to custom log use custom source.

  10. Bookmarked your feed! Thanks!

  11. Josh says:

    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.

  12. swami says:

    does this program work in windows 7. It shows security exception for me.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>