문제

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

도움이 되었습니까?

해결책 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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top