Skip to content

Commit

Permalink
增加异常捕获
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Aug 7, 2022
1 parent ae39808 commit a7e95db
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 0 deletions.
14 changes: 14 additions & 0 deletions EvernoteToNotionChrome/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CefSharp;
using CefSharp.Wpf;
using EvernoteToNotionChrome.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
}
57 changes: 57 additions & 0 deletions EvernoteToNotionChrome/Service/EasyLogManager.cs
Original file line number Diff line number Diff line change
@@ -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; }

/// <summary>
/// 如果日志大于10M则清除
/// </summary>
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();


}


}
}
174 changes: 174 additions & 0 deletions EvernoteToNotionChrome/Utils/SimpleLogger.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Initiate an instance of SimpleLogger class constructor.
/// If log file does not exist, it will be created automatically.
/// </summary>
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);
}
}

/// <summary>
/// Log a DEBUG message
/// </summary>
/// <param name="text">Message</param>
public void Debug(string text)
{
WriteFormattedLog(LogLevel.DEBUG, text);
}

public void Debug(object text)
{
WriteFormattedLog(LogLevel.DEBUG, text == null ? "null" : text.ToString());
}

/// <summary>
/// Log an ERROR message
/// </summary>
/// <param name="text">Message</param>
public void Error(string text)
{
WriteFormattedLog(LogLevel.ERROR, text);
}

public void Error(object text)
{
WriteFormattedLog(LogLevel.ERROR, text == null ? "null" : text.ToString());
}

/// <summary>
/// Log a FATAL ERROR message
/// </summary>
/// <param name="text">Message</param>
public void Fatal(string text)
{
WriteFormattedLog(LogLevel.FATAL, text);
}

/// <summary>
/// Log an INFO message
/// </summary>
/// <param name="text">Message</param>
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));
}


/// <summary>
/// Log a TRACE message
/// </summary>
/// <param name="text">Message</param>
public void Trace(string text)
{
WriteFormattedLog(LogLevel.TRACE, text);
}

/// <summary>
/// Log a WARNING message
/// </summary>
/// <param name="text">Message</param>
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
}
}
}

0 comments on commit a7e95db

Please sign in to comment.