Question

I am currently writing Events Messages on to the event Viewer of the Operating system (windows 7), I am using c# & .net framework 4

When i use the front end to see the messages I have logged, I can see that there is a possibility through the front end to add a task, to a specific message.

My idea is to respond to a specific error on the Event viewer.

Like adding an error Handler as a "task" (as is called on the event viewer)

is this possible programatically ? (I know is possible through the front end)

thanks

Was it helpful?

Solution 2

after I did some research what I was after is :

public class EventViewer()
{
    EventLog _eventlog;

    public void createandreferenceEventViewer(string sLogNameTrimmed)
    {
            //Creates or reference the default Event Viewer
            if (!EventLog.SourceExists(sLogNameTrimmed))
            {                
                EventLog.CreateEventSource(sLogNameTrimmed, sLogNameTrimmed);
                Thread.Sleep(500); // Event viewer need latency to allow being written 
            }
            // Connect to the newly created / existing Log
            _eventlog = new EventLog(sLogNameTrimmed);
            _eventlog.Source = sLogNameTrimmed;
            _eventlog.EntryWritten += WrittenEntryEventHandler;
            _eventlog.EnableRaisingEvents = true;
    }

    void WrittenEntryEventHandler(object source, EntryWrittenEventArgs e)
        {
            switch (e.Entry.EntryType)
            {
                case EventLogEntryType.Error:
                        DO_ERROR_HANDLING_HERE();
                    break;
                case EventLogEntryType.Warning :
                        DO_WARNING_HANDLING_HERE();
                    break;
                default:
                    DO_DEFAULT_HANDLING_HERE();
                    break;
            }
        }
}

where WrittenEntryEventHandler will respond to all entries written to the Event Viewer. then later we can filter or add event handlers per type. Thanks to the ones that took time of reading the question itself

Thanks

OTHER TIPS

The Task Scheduler Managed Wrapper will allow you to do this. Look in particular at the EventTrigger which will allow you to hook in to Windows Events. The examples page has a pretty basic example of it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top