0

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

zSynopsis
  • 4,854
  • 21
  • 69
  • 106

3 Answers3

2

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
  • 11,809
  • 5
  • 57
  • 91
1

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
  • 28,753
  • 28
  • 75
  • 83
0

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.