You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.7 KiB
C#

using System.Threading.Tasks;
using System;
using System.Windows;
using SlnMesnac.RfidUpload.NLog;
namespace SlnMesnac.RfidUpload.UI
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private static readonly LogHelper logger = LogHelper.Instance;
public App()
{
//UI线程未捕获异常处理事件UI主线程
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
//Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//Task异常
}
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
try
{
logger.Error(e.Exception.Message);
HandleException(e.Exception);
}
catch (Exception ex)
{
HandleException(ex);
}
finally
{
e.Handled = true;
}
}
private static void HandleException(Exception ex)
{
logger.Error(ex.Message);
//Logger log = new Logger();
//log.Log(ex.ToString());
}
//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
var exception = e.ExceptionObject as Exception;
if (exception != null)
{
HandleException(exception);
}
}
catch (Exception ex)
{
HandleException(ex);
}
finally
{
//ignore
}
}
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
try
{
var exception = e.Exception as Exception;
if (exception != null)
{
HandleException(exception);
}
}
catch (Exception ex)
{
HandleException(ex);
}
finally
{
e.SetObserved();
}
}
}
}