簡體   English   中英

事件日志寫入錯誤

[英]Event log write error

這很簡單,我想寫一些事情日志。

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);
        }
    }

例外是:

   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)

錯誤消息告訴您確切的錯誤。 您在應用程序日志中注冊了事件源IvrService ,而不是IvrServiceLog。 System.Diagnostics.EventLog.SourceExists驗證源是否存在,但不驗證特定日志。

我的猜測是你最初使用應用程序日志注冊了它,然后將其更改為寫入IvrServiceLog

要清理您的開發機器,您只需運行以下代碼,然后您的代碼就可以繼續運行。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM