private static void appDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var exc = e.ExceptionObject as Exception; if (exc == null) { string exceptionObjectType = "NULL", exceptionObjectToString = "NULL"; if (e.ExceptionObject != null) //ExceptionObject was set but not an exception type { exceptionObjectType = e.ExceptionObject.GetType().FullName; exceptionObjectToString = e.ExceptionObject.ToString(); } var message = $"Unhandled Exception in AppDomain. [IsTerminating: {e.IsTerminating}, ExceptionObjectType: {exceptionObjectType}, ExceptionObject: {exceptionObjectToString}]"; WriteError(message, typeof(Log).FullName, MethodBase.GetCurrentMethod().Name); WriteSystem(message); } else { var message = $"Unhandled Exception in AppDomain. [IsTerminating: {e.IsTerminating}]"; WriteError(message, typeof(Log).FullName, MethodBase.GetCurrentMethod().Name, exc); WriteSystem(new LogEntry(Thread.CurrentThread.ManagedThreadId, DateTime.Now, typeof(Log).FullName, MethodBase.GetCurrentMethod().Name, LogEntryType.Error, exc, message)); } BackgroundThreadQueue.Exit(TimeSpan.FromSeconds(5)); }
private static void DisposeAllWriters() { foreach (var writer in DirectWriters) { writer.Dispose(); } DirectWriters.Clear(); BackgroundThreadQueue.Exit(TimeSpan.FromSeconds(1)); }
/// <summary> /// Flush Queue and shutdowns all Logging. /// Waits for the given timeout before forcing the shutdown. /// </summary> public static bool Exit(TimeSpan?timeout = null) { if (timeout == null) { timeout = new TimeSpan(0, 0, 10); } lock (LockObject) { SendToWriters(new LogEntry(Thread.CurrentThread.ManagedThreadId, DateTime.Now, typeof(BackgroundThreadQueue).FullName, MethodBase.GetCurrentMethod().Name, LogEntryType.Trace, null, "BackgroundThreadQueue Exit Requested")); var result = true; result = BackgroundThreadQueue.Exit(timeout.Value); AppDomain.CurrentDomain.UnhandledException -= appDomain_UnhandledException; DisposeAllWriters(); return(result); } }