Why does my C # Windows service stop working without writing messages to the application event log?

I am new to windows services. I created an installer for my C # Windows service, and the installation on the server (Windows Server 2003) seems to have worked. When it starts, it writes Service started successfully to the log. When he stopped, he writes Service stopped successfully . However, sometimes the service stops working without writing anything to the log, so I start it manually. When I look at the log later, it says Service started successfully , as expected. It is strange to see that two times in a row in a magazine is that there is clearly no entry in which the service somehow stopped working.

What could be the reason for this? I have a service configured as automatic, and it has been installed for all users. I got the impression that this means that the service starts automatically whenever the machine boots. How can I find out why it stopped? Are cases that the failure is automatically recorded in the event log, or do I have to handle the exceptions so that they register their own cause of the failure?

Edit: Additional information:

  • I set it to log in as a local system account
  • In the "Recovery options" section, I configured it to reboot on the first failure. I have nothing for the second or subsequent failures.

Update:. The responder recommended a global exception handler. Although I will not apply this as a permanent fix, it will at least help me figure out where the problem is. I really tested this with the installed service and it works. I found out that unhandled exceptions actually destroy the service without writing anything to the log at all. I thought that he would at least report some application error, but it is not.

 static void Main() { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); //other code here } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Utilities.WriteIt(e.ExceptionObject as Exception); } 
+6
source share
3 answers

It's always better to handle exceptions. At least use a global exception handler and write it to the log file

+4
source

It appears that your service terminates unexpectedly without performing any exception handling and / or logging operations. Windows Services does not automatically record exceptions from the Event Log - you decide to handle the exceptions and (if they are fatal) write them somewhere so that you can diagnose the problem.

At the very least, I would recommend a log file somewhere (perhaps in a service executable folder or, preferably, somewhere else, which is easy to obtain and will not work with permissions) and the standard logging method, exception handlers call to write your posts.

+3
source

If the service unexpectedly terminates due to some kind of exception, I'm not sure if it will automatically go to the event log.

I would highly recommend a set of logs like log4net for more thorough logging. You will be able to provide many levels of registration (debug traces to see if you have reached some code, traces of information for important events, error errors for journal exceptions).

You can look here for an example of EventLogAppender. However, I would suggest starting with getting FileAppender, one of the easiest logs to create, work first, and then add a second application for the event log.

0
source

Source: https://habr.com/ru/post/916249/


All Articles