繁体   English   中英

WPF应用程序使用System.ObjectDisposedExceptions填充Windows事件日志

[英]WPF Application Filling Windows Event Log With System.ObjectDisposedExceptions

我有一个正在生产的WPF应用程序,由于System.ObjectDisposedException,该应用程序正在用AppCrash错误填充Windows应用程序事件日志。 AppCrash专门指向我的应用程序SampleApplication.exe。 另外,该应用程序在无法连接到网络的系统上运行(因此没有远程调试的可能性),我很难找到问题所在。

我附加了两个事件处理程序,以捕获和记录所有未处理的异常。 这些适用于所有其他异常。 我不确定这些System.ObjectDisposedExceptions在进入事件日志之前如何被捕获,以及为什么应用程序实际上没有崩溃。 它保持运行正常。

以下是应用程序的异常事件处理程序以及将它们连接起来的App构造函数:

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);
}

我不确定这些是否与我的问题相关,即:

如何捕获这些异常并记录详细信息,而不是填充Windows应用程序事件日志? 而且,该应用程序继续可以正常运行,因此尽管它们是AppCrash条目,但该应用程序实际上并未崩溃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM