I have an in-production WPF application that is filling the Windows Application event log with AppCrash errors due to a System.ObjectDisposedException. The AppCrash specifically points to my application, SampleApplication.exe. In addition, the application is running on a system that cannot be connected to a network (so no remote debugging possibilities) and I am having a difficult time finding the issue.
I have two event handlers attached to catch and log all unhandled exceptions. These are working for all other exceptions. I'm not sure how these System.ObjectDisposedExceptions are not being caught before they get to the event log and, also, why the application does not actually crash. It keeps running just fine.
Here are the application's exception event handlers and the App constructor that hooks them up:
public App() : base()
{
System.Windows.Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
}
void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Console.WriteLine(e.Exception.Message);
Console.WriteLine(e.Exception.StackTrace);
LogException(e.Exception);
e.Handled = true;
}
void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
if (unhandledExceptionEventArgs.ExceptionObject is Exception)
{
var e = (Exception)unhandledExceptionEventArgs.ExceptionObject;
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
LogException((Exception)unhandledExceptionEventArgs.ExceptionObject);
}
}
void LogException(Exception e)
{
Exception tmp = e;
while (tmp.InnerException != null)
tmp = tmp.InnerException;
string errorMessage = string.Format("An unhandled exception occurred: {0}", tmp.Message);
System.Windows.MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
EventLogService.AddLog(EventLogSourceTypes.Error, errorMessage, DateTime.UtcNow, e.StackTrace);
}
I'm not sure that those are even relevant to my question, which is:
How can I catch these exceptions and document the details instead of them filling up the Windows Application event log? And, the application continues to run just fine, so despite them being AppCrash entries, the app is not actually crashing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.