日志不会进入在 Windows 服务中创建的自定义事件日志

Logs not going to CustomEventLog created in a WindowsService

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

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

Installers.Add(logInstaller);

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

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

Running a transacted installation.

Beginning the Install phase of the installation. See the contents of the log file for the D:\MyService\MyService\bin\Release\MyService.exe assembly's progress. The file is located at D:\MyService\MyService\bin\Release\MyService.InstallLog.

An exception occurred during the Install phase. System.ArgumentException: Source MyServices already exists on the local computer.

The Rollback phase of the installation is beginning. See the contents of the log file for the D:\MyService\MyService\bin\Release\MyService.exe assembly's progress. The file is located at D:\MyService\MyService\bin\Release\MyService.InstallLog.

The Rollback phase completed successfully.

The transacted install has completed.

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

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

有人能帮我看看我做错了什么吗?

添加新的日志源时,必须重新启动服务器才能正确找到日志源。

您还需要安装程序知道日志源是否已经存在。

所以在添加之前添加一个快速检查日志源,并在第一次安装后重新启动服务器以使日志源工作。