diff --git a/EvernoteToNotionChrome/App.xaml.cs b/EvernoteToNotionChrome/App.xaml.cs index d0056c9..67e45f2 100644 --- a/EvernoteToNotionChrome/App.xaml.cs +++ b/EvernoteToNotionChrome/App.xaml.cs @@ -1,5 +1,6 @@ using CefSharp; using CefSharp.Wpf; +using EvernoteToNotionChrome.Service; using System; using System.Collections.Generic; using System.Configuration; @@ -25,6 +26,9 @@ public partial class App : Application //Add Custom assembly resolver AppDomain.CurrentDomain.AssemblyResolve += Resolver; + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + DispatcherUnhandledException += Current_DispatcherUnhandledException; + //Any CefSharp references have to be in another method with NonInlining // attribute so the assembly rolver has time to do it's thing. InitializeCefSharp(); @@ -100,6 +104,16 @@ private static Assembly Resolver(object sender, ResolveEventArgs args) + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + Exception ex = (Exception)e.ExceptionObject; + EasyLogManager.Logger.Error("Error#1 " + ex?.Message + Environment.NewLine + ex?.InnerException?.ToString()); + } + private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) + { + EasyLogManager.Logger.Error("Error#2 " + e?.Exception?.Message + Environment.NewLine + e?.Exception?.InnerException?.ToString()); + e.Handled = true; + } } } diff --git a/EvernoteToNotionChrome/Service/EasyLogManager.cs b/EvernoteToNotionChrome/Service/EasyLogManager.cs new file mode 100644 index 0000000..891ba87 --- /dev/null +++ b/EvernoteToNotionChrome/Service/EasyLogManager.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Threading; +using System.IO; +using System.Diagnostics; +using EvernoteToNotionChrome.Utils; + +namespace EvernoteToNotionChrome.Service +{ + public class EasyLogManager + { + private static string _logFilename = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".log"; + + public static SimpleLogger Logger { set; get; } + + /// + /// 如果日志大于10M则清除 + /// + static EasyLogManager () + { + if (File.Exists(_logFilename)) + { + FileInfo finfo = new FileInfo(_logFilename); + if (!finfo.Exists) + { + FileStream fs = File.Create(_logFilename); + fs.Close(); + } + else + { + try + { + if (finfo.Length > 1024 * 1024 * 20) + { + File.Delete(_logFilename); + FileStream fs = File.Create(_logFilename); + fs.Close(); + } + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + } + } + + Logger = new SimpleLogger(); + + + } + + + } +} diff --git a/EvernoteToNotionChrome/Utils/SimpleLogger.cs b/EvernoteToNotionChrome/Utils/SimpleLogger.cs new file mode 100644 index 0000000..54e122a --- /dev/null +++ b/EvernoteToNotionChrome/Utils/SimpleLogger.cs @@ -0,0 +1,174 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EvernoteToNotionChrome.Utils +{ + public class SimpleLogger + { + private const string FILE_EXT = ".log"; + private readonly object fileLock = new object(); + private readonly string datetimeFormat; + private readonly string logFilename; + + /// + /// Initiate an instance of SimpleLogger class constructor. + /// If log file does not exist, it will be created automatically. + /// + public SimpleLogger() + { + datetimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; + logFilename = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + FILE_EXT; + + // Log file header line + string logHeader = logFilename + " is created."; + if (!System.IO.File.Exists(logFilename)) + { + WriteLine(System.DateTime.Now.ToString(datetimeFormat) + " " + logHeader); + } + } + + /// + /// Log a DEBUG message + /// + /// Message + public void Debug(string text) + { + WriteFormattedLog(LogLevel.DEBUG, text); + } + + public void Debug(object text) + { + WriteFormattedLog(LogLevel.DEBUG, text == null ? "null" : text.ToString()); + } + + /// + /// Log an ERROR message + /// + /// Message + public void Error(string text) + { + WriteFormattedLog(LogLevel.ERROR, text); + } + + public void Error(object text) + { + WriteFormattedLog(LogLevel.ERROR, text == null ? "null" : text.ToString()); + } + + /// + /// Log a FATAL ERROR message + /// + /// Message + public void Fatal(string text) + { + WriteFormattedLog(LogLevel.FATAL, text); + } + + /// + /// Log an INFO message + /// + /// Message + public void Info(string text) + { + WriteFormattedLog(LogLevel.INFO, text); + } + + public void Info(object text) + { + WriteFormattedLog(LogLevel.INFO, text == null ? "null" : text.ToString()); + } + + public void Info(string text, params object[] args) + { + WriteFormattedLog(LogLevel.INFO, string.Format(text, args)); + } + + + /// + /// Log a TRACE message + /// + /// Message + public void Trace(string text) + { + WriteFormattedLog(LogLevel.TRACE, text); + } + + /// + /// Log a WARNING message + /// + /// Message + public void Warning(string text) + { + WriteFormattedLog(LogLevel.WARNING, text); + } + + private void WriteLine(string text, bool append = false) + { + try + { + if (string.IsNullOrEmpty(text)) + { + return; + } + lock (fileLock) + { + using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8)) + { + writer.WriteLine(text); + } + } + } + catch + { + throw; + } + } + + private void WriteFormattedLog(LogLevel level, string text) + { + string pretext; + switch (level) + { + case LogLevel.TRACE: + pretext = System.DateTime.Now.ToString(datetimeFormat) + " [TRACE] "; + break; + case LogLevel.INFO: + pretext = System.DateTime.Now.ToString(datetimeFormat) + " [INFO] "; + break; + case LogLevel.DEBUG: + pretext = System.DateTime.Now.ToString(datetimeFormat) + " [DEBUG] "; + break; + case LogLevel.WARNING: + pretext = System.DateTime.Now.ToString(datetimeFormat) + " [WARNING] "; + break; + case LogLevel.ERROR: + pretext = System.DateTime.Now.ToString(datetimeFormat) + " [ERROR] "; + break; + case LogLevel.FATAL: + pretext = System.DateTime.Now.ToString(datetimeFormat) + " [FATAL] "; + break; + default: + pretext = ""; + break; + } + + System.Diagnostics.Debug.WriteLine(pretext + text); + + WriteLine(pretext + text, true); + } + + [System.Flags] + private enum LogLevel + { + TRACE, + INFO, + DEBUG, + WARNING, + ERROR, + FATAL + } + } +}