diff --git a/LiaoTian_Cup/App.xaml.cs b/LiaoTian_Cup/App.xaml.cs index 4033715..46f6de8 100644 --- a/LiaoTian_Cup/App.xaml.cs +++ b/LiaoTian_Cup/App.xaml.cs @@ -1,4 +1,6 @@ -using System; +using LiaoTian_Cup.Helper; +using log4net; +using System; using System.Collections.Generic; using System.Configuration; using System.Data; @@ -13,5 +15,85 @@ namespace LiaoTian_Cup /// public partial class App : Application { + private static readonly ILog log = LogManager.GetLogger(typeof(App)); + protected override void OnStartup(StartupEventArgs e) + { + LogHelper.InitLog4Net(); + RegisterEvents(); + base.OnStartup(e); + } + + private void RegisterEvents() + { + //Task线程内未捕获异常处理事件 + TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; + + //UI线程未捕获异常处理事件(UI主线程) + DispatcherUnhandledException += App_DispatcherUnhandledException; + + //非UI线程未捕获异常处理事件(例如自己创建的一个子线程) + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + } + + //Task线程内未捕获异常处理事件 + private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) + { + try + { + if (e.Exception is Exception exception) + { + HandleException(exception); + } + } + catch (Exception ex) + { + HandleException(ex); + } + finally + { + e.SetObserved(); + } + } + + //非UI线程未捕获异常处理事件(例如自己创建的一个子线程) + private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + try + { + if (e.ExceptionObject is Exception exception) + { + HandleException(exception); + } + } + catch (Exception ex) + { + HandleException(ex); + } + } + + //UI线程未捕获异常处理事件(UI主线程) + private static void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) + { + try + { + HandleException(e.Exception); + } + catch (Exception ex) + { + HandleException(ex); + } + finally + { + e.Handled = true; + } + } + + //日志记录 + private static void HandleException(Exception ex) + { + //记录堆栈和错误日志 + LogHelper.WriteInfoLog(ex.ToString()); + Current.Shutdown(); + } } } diff --git a/LiaoTian_Cup/AssemblyInfo.cs b/LiaoTian_Cup/AssemblyInfo.cs index e8af039..4297849 100644 --- a/LiaoTian_Cup/AssemblyInfo.cs +++ b/LiaoTian_Cup/AssemblyInfo.cs @@ -8,4 +8,6 @@ ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located //(used if a resource is not found in the page, // app, or any theme specific resource dictionaries) -)] \ No newline at end of file +)] + +[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)] \ No newline at end of file diff --git a/LiaoTian_Cup/Dictionary/I18n/Lang.resx b/LiaoTian_Cup/Dictionary/I18n/Lang.resx index c7ca5b5..e6ec689 100644 --- a/LiaoTian_Cup/Dictionary/I18n/Lang.resx +++ b/LiaoTian_Cup/Dictionary/I18n/Lang.resx @@ -196,7 +196,7 @@ you need to pick commander - 聊天杯 LiaoTian_Cup v0.2.0 - Programmed by B1ackSand + 聊天杯 LiaoTian_Cup v0.2.1 - Programmed by B1ackSand 3 mutators mode diff --git a/LiaoTian_Cup/Dictionary/I18n/Lang.zh.resx b/LiaoTian_Cup/Dictionary/I18n/Lang.zh.resx index b7be19f..11833c3 100644 --- a/LiaoTian_Cup/Dictionary/I18n/Lang.zh.resx +++ b/LiaoTian_Cup/Dictionary/I18n/Lang.zh.resx @@ -196,7 +196,7 @@ 未选择指挥官 - 聊天杯 LiaoTian_Cup v0.2.0 - Programmed by B1ackSand + 聊天杯 LiaoTian_Cup v0.2.1 - Programmed by B1ackSand 3因子模式 diff --git a/LiaoTian_Cup/Helper/CSVKit.cs b/LiaoTian_Cup/Helper/CSVKit.cs index f178ecb..f96c08e 100644 --- a/LiaoTian_Cup/Helper/CSVKit.cs +++ b/LiaoTian_Cup/Helper/CSVKit.cs @@ -8,13 +8,14 @@ namespace LiaoTian_Cup.Helper { class CSVKit { + // TODO 读写锁的问题存在 //两个实现 public static List Csv2Dt(string filePath, List list) { try { StreamReader reader = new StreamReader(filePath, Encoding.UTF8, false); - while (reader.Peek()>0) + while (reader.Peek() > 0) { string str = reader.ReadLine(); string[] split = str.Split(','); diff --git a/LiaoTian_Cup/Helper/LogHelper.cs b/LiaoTian_Cup/Helper/LogHelper.cs new file mode 100644 index 0000000..c4e3969 --- /dev/null +++ b/LiaoTian_Cup/Helper/LogHelper.cs @@ -0,0 +1,39 @@ +using log4net.Config; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LiaoTian_Cup.Helper +{ + public class LogHelper + { + public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo"); + public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror"); + + public static void InitLog4Net() + { + var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); + XmlConfigurator.ConfigureAndWatch(logCfg); + loginfo.Info("----- 日志初始化 -----"); + } + + public static void WriteInfoLog(string info) + { + if (loginfo.IsInfoEnabled) + { + loginfo.Info(info); + } + } + + public static void WriteErrLog(string info, Exception ex) + { + if (logerror.IsErrorEnabled) + { + logerror.Error(info, ex); + } + } + } +} diff --git a/LiaoTian_Cup/LiaoTian_Cup.csproj b/LiaoTian_Cup/LiaoTian_Cup.csproj index 5d7851b..c12d9ac 100644 --- a/LiaoTian_Cup/LiaoTian_Cup.csproj +++ b/LiaoTian_Cup/LiaoTian_Cup.csproj @@ -9,7 +9,7 @@ win-x64 true Resources\Logo\icon.ico - 0.2.0 + 0.2.1 B1ackSand app.manifest @@ -26,6 +26,7 @@ + @@ -165,6 +166,12 @@ + + + Always + + + Always @@ -179,6 +186,7 @@ + diff --git a/LiaoTian_Cup/log4net.config b/LiaoTian_Cup/log4net.config new file mode 100644 index 0000000..26cd5c1 --- /dev/null +++ b/LiaoTian_Cup/log4net.config @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file