Windows服务中的自定义事件日志记录

时间:2015-09-04 08:30:46

标签: windows-services event-log installutil eventlog-source custom-eventlog

在项目安装程序中,我正在创建自定义事件日志。但是当我的服务启动时,我的所有日​​志都会转到"应用程序"而不是我的自定义日志。下面是我添加到安装程序的代码。

// Create Event Source and Event Log     
EventLogInstaller logInstaller = new EventLogInstaller();
logInstaller.Source = "MyServices";
logInstaller.Log = "MyService Events";

Installers.Add(logInstaller);

此外,服务名称是MyService.exe。

当我卸载并重新安装该服务时,安装失败并显示以下安装日志;

  

运行事务安装。

     

开始安装的安装阶段。看到的内容   D:\ MyService \ MyService \ bin \ Release \ MyService.exe的日志文件   集会的进步。该文件位于   d:\为MyService \为MyService \ BIN \发布\ MyService.InstallLog

     

安装阶段发生异常。   System.ArgumentException:源MyServices已存在于   本地电脑。

     

安装的回滚阶段正在开始。看到内容   的日志文件   D:\ MyService \ MyService \ bin \ Release \ MyService.exe程序集的进度。   该文件位于   d:\为MyService \为MyService \ BIN \发布\ MyService.InstallLog

     

回滚阶段已成功完成。

     

已完成交易安装。

这就是我写日志条目的方式;

  

EventLog.WriteEntry(" MyServices",logMessage,logType);

有人可以帮我解决我的错误。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题并通过以下方式解决了:

  1. 停止当前服务。
  2. 关闭任务管理器、服务管理工具和 Windows 事件查看器。
  3. 使用 intallutil.exe /u 卸载当前服务。
  4. 删除当前服务的事件日志注册表项。(这很重要!) (\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\yourCurrentLogName) 您可以在 Windows 事件查看器中找到您当前的日志并确认您的删除。
  5. 使用 installutil.exe 重新安装服务。
  6. 重新启动 Windows。(这也很重要!) 我不确定是否有必要。但即使我退出,我也不断收到错误消息。 自从我重新启动 Windows 以来,一切都很好。 (也许 AWS 缓存了一些东西?)