-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae39808
commit a7e95db
Showing
3 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
|
||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |