.

AppDomain Exception Handling

This is how you hook it up:

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

This is an implementation of it:

/// <summary>
/// Handles the UnhandledException event of the CurrentDomain control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
/// <exception cref="System.NotImplementedException"></exception>
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    var ex = (Exception)e.ExceptionObject;

    ServiceBase serviceBase = (ServiceBase)this.workerService;

    string message = string.Format(
                                    "Message: {0}\nInner Exception: {1}\nStack Trace:{2}",
                                    ex.Message,
                                    ex.InnerException != null ? ex.InnerException.ToString() : string.Empty,
                                    ex.StackTrace ?? string.Empty);

    serviceBase.EventLog.WriteEntry(message, EventLogEntryType.Error);
}