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
- The service is built by release mode.
- Install by both
InstallUtil
andManagedInstallerClass.InstallHelper()
- 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.
- The service is running as local system.
- The config is alright.
- 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?