Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service log writing on system shutdown / restart

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

like image 785
Paul Zahra Avatar asked Oct 23 '25 15:10

Paul Zahra


1 Answers

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" :

enter image description here

like image 144
Perfect28 Avatar answered Oct 26 '25 04:10

Perfect28



Donate For Us

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