[英]Log unhandled exception
I have been trying to log this exception for days but cant figure it out. 我已经尝试记录该异常数天了,但无法弄清楚。 So I thought I post it here with the hope that someone could help.
因此,我以为我将其发布在这里,希望有人能提供帮助。 I have also posted some of my code that I'm currently using to catch unhandled exceptions but the exception never showed up in the log file.
我还发布了一些我当前用于捕获未处理的异常的代码,但是该异常从未出现在日志文件中。
I would appreciate if someone could help me. 如果有人可以帮助我,我将不胜感激。
Problem Event Name: APPCRASH
问题事件名称:APPCRASH
Application Name: Myapp.exe应用名称:Myapp.exe
Application Version: 1.0.0.0应用版本:1.0.0.0
Application Timestamp: 52e9aab8应用时间戳:52e9aab8
Fault Module Name: clr.dll故障模块名称:clr.dll
Fault Module Version: 4.0.30319.18052故障模块版本:4.0.30319.18052
Fault Module Timestamp: 5173c26b故障模块时间戳:5173c26b
Exception Code: c00000fd异常代码:c00000fd
Exception Offset: 00372b52异常偏移量:00372b52
OS Version: 6.1.7601.2.1.0.272.7操作系统版本:6.1.7601.2.1.0.272.7
Locale ID: 1033区域设置ID:1033
Additional Information 1: 5cff附加信息1:5cff
Additional Information 2: 5cfff2c5825852a6f100872ec6f038a2附加信息2:5cfff2c5825852a6f100872ec6f038a2
Additional Information 3: a2a3附加信息3:a2a3
Additional Information 4: a2a3734c473189c5efabc88b5081d05a附加信息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());
}
For handling unhandled exceptions, please create a method like this 为了处理未处理的异常,请创建这样的方法
protected TResult LogFailedOperations<TResult>(Func<TResult> action)
{
try
{
return action();
}
catch (Exception ex)
{
// Logic for logging from ex.Message
}
}
Also, when you perform any method, just enclose that method around this LogFailedOperations
method like these examples - 另外,当您执行任何方法时,只需将这些方法围绕在
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 */ });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.