【问题标题】:Where should be custom EventLog created?应该在哪里创建自定义 EventLog?
【发布时间】:2017-01-20 09:58:17
【问题描述】:

我有一个 Windows 服务。我正在努力让 EventLog 正常工作。

在 Windows 服务的构造函数中我这样做:

    public MyService()
    {
        InitializeComponent();

        AutoLog = false;
        if (!EventLog.SourceExists(ServiceName))
        {
            EventSourceCreationData creationData = new EventSourceCreationData(ServiceName, ServiceName);
            EventLog.CreateEventSource(creationData);
        }
    }

运行该服务后,我没有收到任何异常,但在 Event ViewerApplication and Services Logs 下看不到任何内容! (即使在计算机重置后)。

我检查了我的注册表,我的服务出现在

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MyService

不在这里:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\MyService

我通过以管理员身份打开的 Visual Studio 的 VS2015 开发人员命令提示符安装 Windows 服务。

为什么在那儿?为什么不在事件日志中?为什么我在事件查看器中看不到它?

谢谢!

【问题讨论】:

  • ServiceName 变量的值是多少?
  • @Vijai "MyService" 重要吗?

标签: c# .net logging windows-services event-viewer


【解决方案1】:

使用以下代码,在创建之前检查源是否存在。在此处检查命名约定:https://msdn.microsoft.com/en-us/library/2awhba7a.aspx

string machineName = "MyRemoteServerName";
string source = "MyCustomApp";
string logName = "Application";//can be Application, System, or a custom log name.

if (EventLog.SourceExists(logName, machineName))
   return;

EventSourceCreationData eventSourceCreationData = new EventSourceCreationData(source, logName);

eventSourceCreationData.MachineName = machineName;

EventLog.CreateEventSource(eventSourceCreationData);

参考:https://msdn.microsoft.com/en-us/library/system.diagnostics.eventsourcecreationdata(v=vs.110).aspx

【讨论】:

  • machineName 是必须的吗?除此之外,您只是复制并粘贴了我的代码。你为什么要这么做?
猜你喜欢
  • 2012-08-24
  • 2015-11-09
  • 2014-08-14
  • 1970-01-01
  • 2011-08-14
  • 2020-09-13
  • 2019-06-07
  • 1970-01-01
相关资源
最近更新 更多