4

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

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • I'm not 100% sure as I'm not a services guy, but I don't think `OnShutdown` always covers restarts. Try the `SessionEnded` method instead [shown here](http://stackoverflow.com/questions/5202119/detect-shutdown-in-window-service). – Equalsk Nov 26 '15 at 16:06
  • @Equalsk Thanks for your help but I forgot to put in the question text that it isn't just restart... shutdown also does not work... I've edited it now... the title did say shutdown / restart – Paul Zahra Nov 26 '15 at 16:34
  • Hmm... testing... it looks like it could be due to which account it is running under... https://social.msdn.microsoft.com/Forums/vstudio/en-US/5b7d82c3-e7d6-471f-b085-d46e7de9c737/service-not-doing-onstop-or-onshutdown-when-windows-shuts-down?forum=csharpgeneral – Paul Zahra Nov 26 '15 at 16:39
  • Nope that made no difference... still not writing to log using an (domain) admin account, logging on as system or logging on as service... nothing is written. grrrr – Paul Zahra Nov 26 '15 at 16:58

1 Answers1

2

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

Perfect28
  • 11,089
  • 3
  • 25
  • 45
  • 1
    Thanks for your suggestion. I already use ServiceInstaller so I just added 'EventLog' to ServiceDependedOn, installed the service on my dev machine, shutdown windows... and nothing is written to the event log :S ... Documentation implies it's all about service dependencies required for the service to start... nothing about shutting down etc. – Paul Zahra Nov 27 '15 at 09:50
  • Just making sure... is EventLog the name of the windows event log service? or a generic name? or is it supposed to be the name of my instance of Event Log ? – Paul Zahra Nov 27 '15 at 09:57
  • It should be. Have you checked in the dependecies tab on your service properties to see if Windows Event log is there ? – Perfect28 Nov 27 '15 at 10:05
  • It should be... It should be what?... I manually put EventLog using the visual editor. – Paul Zahra Nov 27 '15 at 10:15
  • 1
    Ah k thx, just checked the service entry using Computer Management and it says, just as your image, Windows Event Log as a dependency... alas still not working. – Paul Zahra Nov 27 '15 at 12:51
  • It also doesn't work for me. – Mike Keskinov Mar 19 '23 at 09:45