0

I'm developing a new windows service and I follow the steps provided in this instruction.

In the service class, I do pretty much the same things as the article

static EventLog _eventLog;

public CMSMetadata()
{
    InitializeComponent();

    _eventLog = new EventLog();
    if (!EventLog.SourceExists("CMSMetadata_Processing"))
    {
        EventLog.CreateEventSource(new EventSourceCreationData("CMSMetadata_Processing", "CMSMetadata"));
    }
    _eventLog.Source = "CMSMetadata_Processing";
    _eventLog.Log = "CMSMetadata";
}

protected override void OnStart(string[] args)
{
    _eventLog.WriteEntry("In OnStart.", EventLogEntryType.Information);
}

After I installed and try to start the service, it shows me the following error

Error 1053: the service did not respond to the start or control request in a timely fashion

And I also notice CreateEventSource() does not create the event log entry when I look for it in the event viewer.

I found this SO post is discussing the 1053 error I'm facing but none of the solutions works for me.

I have confirmed/tried

  1. The service is built by release mode.
  2. Install by both InstallUtil and ManagedInstallerClass.InstallHelper()
  3. The framework version matches what I have installed, actually, I tried 4.5.2 and 4.7.2 in case somehow it really has something to do with the framework.
  4. The service is running as local system.
  5. The config is alright.
  6. If I remove every code related to event log the service can be launched successfully.

Then I think fine if the service somehow can't create event log entry properly maybe I can create the entry in advance as a workaround.

However, in this way, I won't even able to install the service. The install log (CMSMetadata.InstallLog) as below indicates somehow the installation will create the event source no matter I'm using CreateEventSource() or not.

Installing assembly 'C:\temp\GisSoftware\CMSMetadata\CMSMetadata.exe'.
Affected parameters are:
   logtoconsole = 
   logfile = C:\temp\GisSoftware\CMSMetadata\CMSMetadata.InstallLog
   assemblypath = C:\temp\GisSoftware\CMSMetadata\CMSMetadata.exe
Installing service CMSMetadata...
Service CMSMetadata has been successfully installed.
Creating EventLog source CMSMetadata in log Application...
Rolling back assembly 'C:\temp\GisSoftware\CMSMetadata\CMSMetadata.exe'.
Affected parameters are:
   logtoconsole = 
   logfile = C:\temp\GisSoftware\CMSMetadata\CMSMetadata.InstallLog
   assemblypath = C:\temp\GisSoftware\CMSMetadata\CMSMetadata.exe
Restoring event log to previous state for source CMSMetadata.
Service CMSMetadata is being removed from the system...
Service CMSMetadata was successfully removed from the system.

To conclude my question, What did I miss to use the event log in windows service?

Circle Hsiao
  • 1,497
  • 4
  • 22
  • 37

1 Answers1

0

It seems the service does create an event log by default. I should probably set AutoLog property of Service to false before generate the Installer.

Find the EventLogInstaller insides the ServiceInstaller and modify the log and source fixed my problem(also remember to remove the eventlog entry creation in service ctor).

public ProjectInstaller()
{
    InitializeComponent();

    EventLogInstaller installer = this.Installers.OfType<ServiceInstaller>().First()
        .Installers.OfType<EventLogInstaller>().First();

    installer.Source = _source;
    installer.Log = _logName;
}

Moreover, it seems better to create eventlog during the installation instead of service ctor since it appears the permission of LocalSystem to access registry is limited.

ref: Easiest language for creating a Windows service

Circle Hsiao
  • 1,497
  • 4
  • 22
  • 37