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

发布于 2024-10-15 16:27:12 字数 262 浏览 8 评论 0原文

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

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

我的问题是安装项目尝试默认创建事件日志源。通过这样做,它会收到一条错误消息(“错误 1001”源 XXX 已存在于本地计算机上)并回滚。

我到处都找过了,但找不到在哪里完成注册或如何将其关闭。

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

I got a setup project that installs a windows service.

We are registering a event log source in a custom log which should be used by the winservice project (how and why is not important).

My problem is that the setup project tries to create an event log source per default. By doing so it get's an error message ("Error 1001" source XXX already exists on local computer) and rolls back.

I have looked everywhere and I cannot find where the registration is done or how I can turn it off.

How can I force the windows service or setup project to NOT create an event log source?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小姐丶请自重 2024-10-22 16:27:12

您可以删除默认的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,并且服务启动/停止事件仍将记录到应用程序日志中。

(来源:serviceInstaller 组件及其默认的 EventLogInstaller)

You can remove the default 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);
            }
        }
    }
}

Alternatively, you can modify the Log property:

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

Now events will be successfully logged to MyLog, and service start/stop events will still be logged to the Application log.

(source: serviceInstaller component and its default EventLogInstaller)

东走西顾 2024-10-22 16:27:12

我猜想当您卸载该服务时,某些内容未正确卸载,特别是在事件日志中。

要恢复重新安装服务的能力,我发现(感谢本文)您需要在注册表 (regedit.exe) 中删除此键:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\YOURSERVICENAME

I guess that when you uninstall the service, something is not correctly uninstalled, expecially in the event log.

To restore the ability to reinstall the service again, I discovered (thanks to this article) you need to remove this key in the registry (regedit.exe):

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\YOURSERVICENAME
潦草背影 2024-10-22 16:27:12

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

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

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

This is only a guess based on my tests.

The installer project (or the WindowService class) creates an event source automatically with the same name as myService.ServiceName. This is most likely because Start/Stop messages are written to the log each time a service is started / stopped. And those messages need a source.

In other words: You do not need to create a source with the same name as the ServiceName as it is done for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文