【问题标题】:Event log write error事件日志写入错误
【发布时间】:2013-11-05 11:40:50
【问题描述】:

很简单,我想在事件日志中写点东西。

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)

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    错误消息准确地告诉您出了什么问题。您将事件源IvrService 注册到应用程序日志,而不是 IvrServiceLog。 System.Diagnostics.EventLog.SourceExists 验证源是否存在,但不是针对特定日志。

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

    要清理您的开发机器,您可以简单地运行以下命令,然后您的代码应该可以继续工作。

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

    【讨论】:

    • @Love 是的,你应该只需要运行我在你的开发机器上发布一次的代码来清理现有日志,然后它应该可以继续工作。
    • 你是对的,但就我而言,当我运行删除事件源的代码时,我的服务运行良好,但我总是要删除事件源,而不仅仅是一次。问题是我在 Application 事件中有一个条目(也许我创建了这个作为应用程序启动服务),并且无法删除。我找到的唯一解决方案是更改事件源名称。
    • 您能否编辑答案以包括 powershell cmdlet Remove-EventLog 也能够使用 -Source 标志删除源。谢谢顺便说一句。
    • 在尝试使用 Remove-EventLog 时,我看到另一个技术上正确但同样​​无用的错误消息:“无法删除事件日志源 'X',因为它与日志名称相同。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多