Unhandled exception handling For a “server…

1 minute read

Unhandled exception handling

_For a "server in a closet", you can simply turn off the default debugger dialog.  You can do this on a Machine-wide, User-wide, or process-wide basis.  The machine-wide and user-wide settings are controlled by:         HKLMSoftwareMicrosoftCOMPlusDbgJitDebugLaunchSetting         HKCUSoftwareMicrosoftCOMPlusDbgJitDebugLanuchSetting_ > > _and the process wide one by the environment variable:_ > > _        COMPLUS_DbgJitDebugLaunchSetting_ > > _The values here are:         0 - ask         1 - never attach a debugger         2 - always attach a debugger_ > > _Set it to 1, and there will be no system-provided dialog._ > >

Also this:

_Personally, I prefer to limit the scope of this kind of setting to per-process (specifically, mine :-), so I go with the environment variable approach. The only glitch is that, there's not managed way to adjust process-wide environment variables (it violates the appdomain isolation story), so you have to resort to p/invoke. But if you're writing a service, then you're going to have the necessary perms, and this kind of setting makes sense anyways._ > > _// Declare this somewhere: [DllImport("kernel32.dll")] static extern bool SetEnvironmentVariable(string varName, string varValue);_ > > _// Do something like this during the service's initial startup: SetEnvironmentVariable("COMPLUS_DbgJitDebugLaunchSetting", "1"); AppDomain.CurrentDomain.UnhandledException +=       new UnhandledExceptionEventHandler(OnUnhandledException);_ > >

Updated: