【问题标题】:Setup project for a windows service and the event logWindows 服务和事件日志的设置项目
【发布时间】:2011-02-02 13:46:43
【问题描述】:

我有一个安装 Windows 服务的安装项目。

我们正在自定义日志中注册一个事件日志源,应该由 winservice 项目使用(如何以及为什么不重要)。

我的问题是安装项目默认尝试创建事件日志源。这样做会得到一条错误消息 ("Error 1001" source XXX already exists on local computer) 并回滚。

我到处寻找,但找不到注册的完成地点或如何将其关闭。

如何强制 Windows 服务或设置项目不创建事件日志源?

【问题讨论】:

    标签: visual-studio setup-project eventlog-source


    【解决方案1】:

    您可以删除默认的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 component and its default EventLogInstaller

    【讨论】:

    • 请注意,在foreach (Installer installer in serviceInstaller1.Installers) 行中包含“serviceInstaller1”很重要。我把它写成foreach (Installer installer in this.Installers)。这构建得很好,安装项目创建了 MSI 文件。但是当我尝试安装该服务时,错误仍然存​​在。只有当我将“this.Installers”更改为“serviceInstaller1.Installers”时,它才修复了错误。
    【解决方案2】:

    我猜当你卸载服务时,有些东西没有正确卸载,特别是在事件日志中。

    要恢复重新安装服务的能力,我发现 (thanks to this article) 你需要在注册表中删除这个键 (regedit.exe):

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\YOURSERVICENAME
    

    【讨论】:

      【解决方案3】:

      这只是基于我的测试的猜测。

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

      换句话说:您不需要像为您创建的那样创建与 ServiceName 同名的源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 2011-03-20
        • 2019-11-18
        相关资源
        最近更新 更多