Last active
September 16, 2023 12:08
-
-
Save ronnieoverby/7568387 to your computer and use it in GitHub Desktop.
wpf global exception handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// somewhere early in App.xaml.cs | |
AppDomain.CurrentDomain.UnhandledException += (s, e) => | |
LogUnhandledException((Exception) e.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException"); | |
DispatcherUnhandledException += (s, e) => | |
LogUnhandledException(e.Exception, "Application.Current.DispatcherUnhandledException"); | |
TaskScheduler.UnobservedTaskException += (s, e) => | |
LogUnhandledException(e.Exception, "TaskScheduler.UnobservedTaskException"); | |
// ... and the actual logging (depends on your logging library) | |
private void LogUnhandledException(Exception exception, string @event) | |
{ | |
_log.Exception(exception) | |
.Data("Event", @event) | |
.Fatal("Unhandled exception"); | |
// wait until the logmanager has written the entry | |
_log.LogManager.FlushEntriesAsOf(DateTimeOffset.Now.AddSeconds(1)); | |
} | |
/* hooray the exception was logged! | |
MyHappyApp.App : Fatal : 11/20/2013 1:44:31 PM -05:00 | |
Unhandled exception | |
Event: AppDomain.CurrentDomain.UnhandledException | |
System.NullReferenceException: Object reference not set to an instance of an object. | |
at MyHappyApp.UserControls.VendorFrequencyCriteria.<DoSearchAsync>b__6() in d:\Code\MyHappyApp\src\MyHappyApp\UserControls\VendorFrequencyCriteria.xaml.cs:line 119 | |
at System.Threading.Tasks.Task`1.InnerInvoke() | |
at System.Threading.Tasks.Task.Execute() | |
--- End of stack trace from previous location where exception was thrown --- | |
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) | |
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) | |
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() | |
at MyHappyApp.UserControls.VendorFrequencyCriteria.<BtnSearchClick>d__0.MoveNext() in d:\Code\MyHappyApp\src\MyHappyApp\UserControls\VendorFrequencyCriteria.xaml.cs:line 92 | |
--- End of stack trace from previous location where exception was thrown --- | |
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(Object state) | |
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) | |
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) | |
at System.Windows.Threading.DispatcherOperation.InvokeImpl() | |
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) | |
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) | |
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) | |
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) | |
at System.Windows.Threading.DispatcherOperation.Invoke() | |
at System.Windows.Threading.Dispatcher.ProcessQueue() | |
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) | |
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) | |
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) | |
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) | |
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) | |
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) | |
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) | |
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) | |
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) | |
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) | |
at System.Windows.Threading.Dispatcher.Run() | |
at System.Windows.Application.RunDispatcher(Object ignore) | |
at System.Windows.Application.RunInternal(Window window) | |
at System.Windows.Application.Run(Window window) | |
at System.Windows.Application.Run() | |
at MyHappyApp.App.Main() in d:\Code\MyHappyApp\src\MyHappyApp\obj\x86\Debug\App.g.cs:line 52 | |
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) | |
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) | |
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() | |
at System.Threading.ThreadHelper.ThreadStart_Context(Object state) | |
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) | |
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) | |
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) | |
at System.Threading.ThreadHelper.ThreadStart() | |
Exception Data: | |
System.Object: | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you, this helped me