Question: 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);
}

```

Answer: 
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.
If a service quits unexpectedly because of some exception, I am not sure it would end up in the Event Log automatically.

I would highly recommend a logging suite like [log4net](http://logging.apache.org/log4net/) for more thorough logging. You'll be able to provide a multitude of logging 'levels' (debug traces to see if you reached some code, info traces for important events, error traces to log exceptions). 

You can look [here](http://logging.apache.org/log4net/release/config-examples.html) for an example of a EventLogAppender. However, I would suggest starting with getting a FileAppender, one of the easiest logs to create, working first and then add a second appender for the Event Log.