【问题标题】:Log unhandled exception记录未处理的异常
【发布时间】:2014-01-30 02:35:25
【问题描述】:

我几天来一直在尝试记录此异常,但无法弄清楚。所以我想我把它贴在这里,希望有人能提供帮助。我还发布了一些我目前用来捕获未处理异常的代码,但异常从未出现在日志文件中。

如果有人可以帮助我,我将不胜感激。

问题事件名称:APPCRASH
应用程序名称:Myapp.exe
应用程序版本:1.0.0.0
申请时间戳:52e9aab8
故障模块名称:clr.dll
故障模块版本:4.0.30319.18052
故障模块时间戳:5173c26b
异常代码:c00000fd
异常偏移量:00372b52
操作系统版本:6.1.7601.2.1.0.272.7
区域设置 ID:1033
附加信息 1:5cff
附加信息 2:5cfff2c5825852a6f100872ec6f038a2
附加信息 3:a2a3
附加信息4:a2a3734c473189c5efabc88b5081d05a

public MainWindow()
{
    Application.Current.DispatcherUnhandledException += CurrentOnDispatcherUnhandledException;
    AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
    Dispatcher.UnhandledException += DispatcherOnUnhandledException;
    InitializeComponent();          
}

private void DispatcherOnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.Exception;
    using (StreamWriter writer = File.AppendText("ExceptionsLog.txt"))
    {
        DateTime time = DateTime.Now;
        writer.WriteLine("Time {0} Exception: {1}", time, e);
        writer.Flush();
    }
    args.Handled = true;
    MessageBox.Show(e.ToString());
}

private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.ExceptionObject;
    using (StreamWriter writer = File.AppendText("ExceptionsLog.txt"))
    {
        DateTime time = DateTime.Now;
        writer.WriteLine("Time {0} Exception: {1} Runtime termination: {2}", time, e.Message, args.IsTerminating);
        writer.Flush();
    }
    MessageBox.Show(e.ToString());
}

private void CurrentOnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.Exception;
    using (StreamWriter writer = File.AppendText("ExceptionsLog.txt"))
    {
        DateTime time = DateTime.Now;
        writer.WriteLine("Time {0} Exception: {1}", time, e);
        writer.Flush();
    }
    args.Handled = true;
    MessageBox.Show(e.ToString());
}

【问题讨论】:

  • c00000fd 是一个堆栈溢出。据推测,堆栈空间不足是导致它无法被记录的原因。

标签: c# .net wpf exception


【解决方案1】:

为了处理未处理的异常,请创建这样的方法

   protected TResult LogFailedOperations<TResult>(Func<TResult> action)
        {
            try
            {
                return action();
            }
            catch (Exception ex)
            {
                // Logic for logging from ex.Message
            }
        }

此外,当您执行任何方法时,只需将该方法括在 LogFailedOperations 方法周围,就像这些示例一样 -

private void MyButton_Click(object sender, RoutedEventArgs e)
{
  var result = LogFailedOperations(() => { /* Your logic for button click */ });
}

private void LoadDataToView()
{
  var result = LogFailedOperations(() => { /* Your logic for laoding data */ });
}

private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
  var result = LogFailedOperations(() => { /* Your logic for slider changed event */ });
}

【讨论】:

  • 你能解释一下为什么 OP 应该这样做吗?
  • 此方法可用于执行将记录异常的 try catch 块中的每个代码块。如果没有完成,那么可能会错过异常。
猜你喜欢
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
  • 2010-09-09
  • 2017-10-08
  • 2020-09-01
相关资源
最近更新 更多