Setup project for a windows service and the event log

旧时模样 提交于 2021-01-27 04:35:52

问题


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?


回答1:


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)




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/4875173/setup-project-for-a-windows-service-and-the-event-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!