事件日志写入错误

20 .net c#

这很简单,我想写一些事情日志.

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

例外是:

   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)
Run Code Online (Sandbox Code Playgroud)

Joh*_*ner 25

错误消息告诉您确切的错误.您IvrService在应用程序日志中注册了事件源,而不是IvrServiceLog.该System.Diagnostics.EventLog.SourceExists源存在,而不是特定的日志验证.

我的猜测是你最初在应用程序日志中注册了这个,然后将其更改为写入IvrServiceLog.

要清理您的开发机器,您只需运行以下代码,然后您的代码就可以继续运行.

System.Diagnostics.EventLog.DeleteEventSource("IvrService");
Run Code Online (Sandbox Code Playgroud)