简体   繁体   中英

Event log write error

It is simple, I want to write something to event log.

protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
        if (!System.Diagnostics.EventLog.SourceExists("IvrService"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "IvrService", "IvrServiceLog");
        }
        EventLog eventLog1 = new System.Diagnostics.EventLog();
        eventLog1.Source = "IvrService";
        eventLog1.Log = "IvrServiceLog";
        try
        {
            eventLog1.WriteEntry("Successfully "+State.Stopped.ToString());
            IvrApplication.StopImmediate();
        }
        catch (Exception ex)
        {
           // eventLog1.WriteEntry(ex.Message);
        }
    }

The exception is:

   Failed to stop service. System.ArgumentException: The source 'IvrService' is not   registered in log 'IvrServiceLog'. (It is registered in log 'Application'.) " The Source   and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property.
   at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName)
   at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
   at System.Diagnostics.EventLog.WriteEntry(String message)

The error message is telling you exactly what is wrong. You have the Event Source IvrService registered with the Application Log, not the IvrServiceLog. The System.Diagnostics.EventLog.SourceExists verifies that the source exists, but not for a particular log.

My guess is that you originally registered this with the Application log and then later changed it to write to the IvrServiceLog .

To clean up your development machine, you could simply run the following and then you code should work going forward.

System.Diagnostics.EventLog.DeleteEventSource("IvrService");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM