CathalMF
CathalMF

Reputation: 10055

C# How to investigate why my service is stopping unexpectedly

My Service works perfectly fine for a random amount of time and then just stops.

I am catching all exceptions that i can see. I have added a try/catch around the service creating to try and get some sort of error but im getting nothing. The service just stops.

        try
        {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new my_service() };
                ServiceBase.Run(ServicesToRun);
        }
        catch (Exception e)
        {
            logEvent(e);   // Log any errors to a .txt file.
        }

The event viewer shows the following:

Faulting application name: my_service.exe, version: 0.9.2.1, time stamp: 0x50717154
Faulting module name: ntdll.dll, version: 6.1.7601.17514, time stamp: 0x4ce7b96e
Exception code: 0xc0000005
Fault offset: 0x00055f99
Faulting process id: 0x%9
Faulting application start time: 0x%10
Faulting application path: %11
Faulting module path: %12
Report Id: %13

Upvotes: 1

Views: 4071

Answers (3)

user1704366
user1704366

Reputation:

You can always do a simple console application, referance the assembly, and call the code from the console app and easily debug it. (easiest option by a long shot).

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 180887

If your service is up and running, logging around Run() won't do you much good, since nothing much executes in that method. It will basically just trigger your service's OnStart method.

The normal case is that your service's OnStart method starts an entirely new Thread to run the actual service. It's in that Thread you need to do the logging. Putting a try/catch around that Thread's entire threadstart method, or subscribing to AppDomain.CurrentDomain.UnhandledException should give you any exceptions your service is generating and not catching so you can log them.

The problem though (which is hard to know since you're not showing the service's code) seems to be that the service crashes in ntdll.dll, which may be a pinvoke (call to an external native DLL), which means you may not get any exceptions to catch. Debugging native crashes is a totally different science.

Upvotes: 3

mbarthelemy
mbarthelemy

Reputation: 12913

It's impossible to guess what went wrong with the information you gave. But you can try to use a trick to catch every uncatched exception, and log it.

Here is how I do in my project:

First, in my Main(), I suscribe to the following event:

AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;

And I define the Handler, which just logs the stacktrace to a file and prints it to the console (though it's irrelevant for you since you run your app as a service):

private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e){
        try{
            Logger.Append(Severity.CRITICAL, "Unhandlable error : "+e.ExceptionObject); 


        }catch{}
        Console.WriteLine("Unhandlable error : "+e.ExceptionObject);
        Environment.Exit(10);
    }

Upvotes: 7

Related Questions