Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my c# Windows service stop running without any messages being written to the application event log?

I am fairly new to Windows services. I created an installer for my c# Windows service and the installation on the server (Windows Server 2003) appears to have worked. When it's started, it writes Service started successfully to the log. When it's stopped, it writes Service stopped successfully. However, sometimes the service stops running without writing anything to the log, so I start it back up manually. When I look at the log afterward, it says Service started successfully as expected. It's weird seeing that in the log twice in a row being that it's obviously missing an entry where the service had somehow stopped running.

What could be the potential causes for this? I have the service set up as automatic and installed it to run for all users. I was under the impression that this means the service starts automatically whenever the machine boots up. How can I find out why it stopped? Do services that crash automatically write to the event log or do I have to handle exceptions in such a way that they log their own reason for the crash?

Edit: Some additional info:

  • I have it set up to log on as Local System Account
  • Under Recovery options, I have it set up to restart on first failure. I don't have anything for second or subsequent failures.

Update: An answerer recommended a global exception handler. While I won't implement this as a permanent fix, it will at least help me figure out where the problem is occurring. I actually tested this with my installed service and it works. I found out that unhandled exceptions actually do crash the service without writing anything to the log at all. I thought it'd at least report some application error, but it doesn't.

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);
}
like image 632
oscilatingcretin Avatar asked May 21 '12 12:05

oscilatingcretin


People also ask

Why my C drive is always full?

Mostly, useless large junk files, big files, huge installed programs, and temporary files are taking up the most space in your system C drive after using your PC for a long time. So the other effective method you can try is to free up hard disk space.

How do you fix C drive full Windows 10?

Free up space with Disk Cleanup In the search box on the taskbar, type disk cleanup, then select it from the results. Select the drive you want to clean up files for, then select OK. Select the check box next to the type of files you want to delete.

What happens if your C drive is full?

When your hard drive is full, your device begins to run slowly. This is because storing data becomes more complex and accessing previous information becomes more time-consuming due to congestion. A full hard drive could also lead to other problems and could make your system crash in the process.


2 Answers

It's always best to handle the exceptions. At least use a global exception handler and write it to a logfile

like image 93
MichelZ Avatar answered Sep 22 '22 17:09

MichelZ


It sounds like your service is failing unexpectedly without doing any form of exception-handling and/or logging. Windows services do not automatically write exceptions to the Event Log - it's up to you to handle exceptions and (if they're fatal) write them out somewhere so that you can diagnose the problem.

At the very least, I'd recommend a logfile somewhere (perhaps in the service executable folder, or preferably somewhere else that's easy to get to and won't run afoul of permissioning issues) and a standard logging method that all your exception-handlers call to write their messages to.

like image 35
Eight-Bit Guru Avatar answered Sep 26 '22 17:09

Eight-Bit Guru