Setup project for a windows service and the event log

7k views Asked by At

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?

3

There are 3 answers

1
user247702 On BEST ANSWER

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)

0
jgauffin On

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.

0
bluish On

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