为Windows服务设置项目和事件日志

7
我有一个安装Windows服务的设置项目。
我们正在自定义日志中注册事件日志源,Winservice项目应该使用它(如何和为什么不重要)。
我的问题是,设置项目默认尝试创建事件日志源。这样做会出现错误消息(“错误1001”源XXX已存在于本地计算机上),并回滚。
我已经到处查找,但找不到注册在哪里或如何关闭它。
如何强制Windows服务或设置项目不创建事件日志源?
3个回答

7
您可以删除默认的EventLogInstaller:
namespace MyService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();

            // Remove the default Event Log Installer
            EventLogInstaller DefaultInstaller = null;
            foreach (Installer installer in serviceInstaller1.Installers)
            {
                if (installer is EventLogInstaller)
                {
                    DefaultInstaller = (EventLogInstaller)installer;
                    break;
                }
            }
            if (DefaultInstaller != null)
            {
                serviceInstaller1.Installers.Remove(DefaultInstaller);
            }
        }
    }
}

或者,您可以修改Log属性:

foreach (Installer installer in serviceInstaller1.Installers)
{
    if (installer is EventLogInstaller)
    {
        ((EventLogInstaller)installer).Log = "MyLog";
        break;
    }
}

现在事件将成功记录到MyLog中,服务启动/停止事件仍将记录到Application日志中。 (来源:serviceInstaller组件及其默认EventLogInstaller

请注意,在行foreach (Installer installer in serviceInstaller1.Installers)中包含“serviceInstaller1”非常重要。我将其写成了foreach (Installer installer in this.Installers)。这样构建很好,安装程序创建了MSI文件。但是当我尝试安装服务时,错误仍然存在。只有当我将“this.Installers”更改为“serviceInstaller1.Installers”时,才能解决错误。 - Simon Elms

3
我猜当你卸载服务时,某些内容没有正确地被卸载,特别是在事件日志中。
为了恢复重新安装该服务的能力,我发现(感谢这篇文章),需要在注册表中删除此键:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\YOURSERVICENAME

2

这只是根据我的测试猜测出来的。

安装程序项目(或WindowService类)会自动创建一个与myService.ServiceName同名的事件源。这很可能是因为每次启动/停止服务时都会将启动/停止消息写入日志。而这些消息需要一个源。

换句话说:您不需要创建与ServiceName相同名称的源,因为它已经为您完成了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接