using MahApps.Metro.Controls; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.IO; namespace Audit { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { /// /// App.xaml 的交互逻辑 /// public App() { this.Startup += new StartupEventHandler(App_Startup); this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException); } System.Threading.Mutex mutex; void App_Startup(object sender, StartupEventArgs e) { bool ret; mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret); if (!ret) { MessageBox.Show("SC:审计管理已启动"); Environment.Exit(0); } } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); //UI线程的异常捕捉 this.DispatcherUnhandledException += App_DispatcherUnhandledException; } private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { string Log_time = null; e.Handled = true; if (e.Exception.InnerException == null) { MessageBox.Show("发生了一个错误!请联系管理员!" + Environment.NewLine + "错误:" + e.Exception.Source + Environment.NewLine + "错误信息:" + e.Exception.Message + Environment.NewLine // + "(4)报错区域:" + e.Exception.StackTrace ); Log_time ="[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]错误:" + e.Exception.Source + "信息:" + e.Exception.Message + "错误区域:" + e.Exception.StackTrace; FileStream fs = new FileStream("C:\\SC\\Audit\\Log\\AuditLog.txt", FileMode.Append, FileAccess.Write); StreamWriter wr = null; wr = new StreamWriter(fs); wr.WriteLine(Log_time); wr.Close(); } else { MessageBox.Show("(1)发生了一个错误!请联系开发人员!" + Environment.NewLine + "(2)错误源:" + e.Exception.InnerException.Source + Environment.NewLine + "(3)错误信息:" + e.Exception.Message + Environment.NewLine + "(4)详细信息:" + e.Exception.InnerException.Message + Environment.NewLine // + "(5)报错区域:" + e.Exception.InnerException.StackTrace ); Log_time = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]错误:" + e.Exception.Source + "信息:" + e.Exception.Message + "错误区域:" + e.Exception.StackTrace; FileStream fs = new FileStream("C:\\SC\\Audit\\Log\\AuditLog.txt", FileMode.Append, FileAccess.Write); StreamWriter wr = null; wr = new StreamWriter(fs); wr.WriteLine(Log_time); wr.Close(); } } } }