事件日志写入错误 [英] Event log write error

查看:188
本文介绍了事件日志写入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很简单,我想写点东西给事件日志。

 保护覆盖无效的onStop()
    {
        // TODO:添加code在这里进行任何必要的拆除,停止你的服务。
        如果(!System.Diagnostics.EventLog.SourceExists(IvrService))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                IvrService,IvrServiceLog);
        }
        事件日志eventLog1 =新System.Diagnostics.EventLog();
        eventLog1.Source =IvrService;
        eventLog1.Log =IvrServiceLog;
        尝试
        {
            eventLog1.WriteEntry(成功+ State.Stopped.ToString());
            IvrApplication.StopImmediate();
        }
        赶上(例外前)
        {
           // eventLog1.WriteEntry(ex.Message);
        }
    }
 

唯一的例外是:

 未能停止服务。 System.ArgumentException:源IvrService没有在日志中记录IvrServiceLog。 (它是注册在日志应用程序)。来源和日志性能必须匹配,或者您可以设置日志为空字符串,它会自动匹配到源属性。
   在System.Diagnostics.EventLogInternal.VerifyAndCreateSource(字符串SOURCENAME,字符串currentMachineName)
   在System.Diagnostics.EventLogInternal.WriteEntry(字符串消息,EventLogEntryType类型的Int32事件ID,Int16的类别,字节[] RAWDATA)
   在System.Diagnostics.EventLog.WriteEntry(字符串消息)
 

解决方案

该错误消息告诉你到底什么是错的。你有事件源 IvrService 的应用程序日志,而不是IvrServiceLog注册。该 System.Diagnostics.EventLog.SourceExists 验证源存在,而不是特定的日志。

我的猜测是,你最初注册这个与应用程序日志,再后来改成了写 IvrServiceLog

要清理你的开发机,你可以简单地运行以下命令,然后你code应该努力前进。

  System.Diagnostics.EventLog.DeleteEventSource(IvrService);
 

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

这篇关于事件日志写入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆