views:

458

answers:

3

Hi all,

I have a console app that will be running through a scheduled task and what i'd like to do is have it write to an Event Log in a catch block. I've tried using

EventLog.WriteEntry("My App Name","Error Message - " ex.ToString() );

but for some reason it is not writing the error. Am i doing something wrong?

Thanks

+1  A: 

This code is from MSDN website in C#, I hope it help you.

using System;
using System.Diagnostics;
using System.Threading;

class MySample{

    public static void Main(){

        // Create the source, if it does not already exist.
        if(!EventLog.SourceExists("MySource")){
            EventLog.CreateEventSource("MySource", "MyNewLog");
            Console.WriteLine("CreatingEventSource");
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");

    }
}
Jonathan
+1  A: 

You need to make sure the event-source exists, e.g.:

if (!EventLog.SourceExists("MySource"))
    EventLog.CreateEventSource("MySource","Application");

See http://support.microsoft.com/kb/307024

Manu
A: 

One thing to note is that there is sometimes a small delay when calling EventLog.CreateEventSource, so you should be aware of that when trying to access the created EventSource immediately after creation.