The issue is that when host machine is restarted (Windows restart) or shutdown (Windows shutdown) nothing is being written to the log.
I have the following in a Windows Service:
protected override void OnStart(string[] args)
{
eventLogUtility.WriteToEventLog("Service has Started.", EventLogEntryType.Information);
}
protected override void OnStop()
{
eventLogUtility.WriteToEventLog("Service has Stopped.", EventLogEntryType.Information);
}
/// <summary>When implemented in a derived class, executes when the system is shutting down.
/// Specifies what should occur immediately prior to the system shutting down.</summary>
protected override void OnShutdown()
{
eventLogUtility.WriteToEventLog("Service has Shutdown.", EventLogEntryType.Information);
}
When the service starts, "Service has Started." is written to the log.
When the service is stopped by me, "Service has Stopped." is written to the log.
P.S. Using .Net 4.5
It's probably because Windows Event Log service shutdowns before your service.
You can solve it by making your Service depends on Windows Event Log using ServiceInstaller.
...
ServiceInstaller serviceInstaller = new ServiceInstaller()
serviceInstaller.ServicesDependedOn = new string [] { "EventLog" };
...
or with the visual editor by adding "EventLog" :

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With