probably at the beach
probably at the beach

Reputation: 15217

Can a c# service crash if there is a catch all

I have a service that is started using a start routine surrounded by a try catch block as below.

    protected override void OnStart(string[] args)
    {
        try
        {
            Program.Start();
        }
        catch (Exception e)
        {
            Logger.Error("Exception during Service Start");
        }
    }

Occasionally on some machines (1/100) it will occasionally print out the last line of Program.Start (a log message) and then fail with no log messages or event log messages. Should this be possible?

Thanks

EDIT: The service does kick off a few other threads but they are encapsulated in the same manner

EDIT: The Logger is a wrapper for log4net - it's working very reliably on a lot (100+) of machines (though yes it possibly the cause)

Upvotes: 3

Views: 205

Answers (5)

Zesty
Zesty

Reputation: 3001

Can you put the call to your Logger component in a try-catch block and let us know what happens? Are you able to monitor your service without the Logger component (i.e., based on whatever ouput the service is supposed to be produce - like DB updates)?

Try writing to the event log directly if the call to your Logger component fails. Put this in a try catch block as well. Since the Logger successfully wrote the last line in Program.Start(), it may be something related to unsuccessfully attempting to dispose a resource or something similar, and this is not getting logged.

Upvotes: 1

WraithNath
WraithNath

Reputation: 18013

The service could crash if an exception is thrown out of a started thread but not caught.

Are you adding a handler to each app domain unhandled exception?

eg:

  AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( CurrentDomain_UnhandledException );

EDIT Also, you have the Application.ThreadException event which i forgot to mention: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx

Upvotes: 3

Evren Kuzucuoglu
Evren Kuzucuoglu

Reputation: 3885

Yes it could be possible. For instance, a StackOverflowException cannot be catched since .NET 2.0 (see the MSDN article about it).

Upvotes: 2

swordfish
swordfish

Reputation: 4995

I am not clear on your question but if you are asking whether your app can crash with a skeleton like that, the chances are very less. Since you have a catch all block almost all kinds of exceptions would be caught and thus your program would end gracefully.

How ever if its a repetive scenario that you are aware of it'd be better to handle it the known way then achieve what you want with an exception as generally execptions for times when something unknown might occur.

Upvotes: 1

Zenwalker
Zenwalker

Reputation: 1919

If the program crashes or an exception is thrown from try block, then for sure youll get Logger.Error log info but not the eventlog from system. If service crash or an unhandled exception occurs, then only eventlog on service is logged.

Upvotes: 2

Related Questions