.netconsole-applicationevent-log

Write to Event Log Windows Scheduled Task


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


Solution

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

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

    See https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.eventlog.createeventsource?view=net-8.0