-
Notifications
You must be signed in to change notification settings - Fork 3
/
Logger.cs
62 lines (52 loc) · 1.9 KB
/
Logger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System;
using System.Collections.Generic;
using System.IO;
namespace BGOverlay
{
public static class Logger
{
private static NLog.Logger logger;
private static readonly Dictionary<string, string> errorCache = new Dictionary<string, string>();
public static void Init()
{
logger = LogManager.GetCurrentClassLogger();
var config = new NLog.Config.LoggingConfiguration();
using (var logfile = new FileTarget("logfile") { FileName = Path.Combine(Directory.GetCurrentDirectory(), "radar.log").ToString(), DeleteOldFileOnStartup = true, Layout = "${longdate} [${level:uppercase=true}] ${message:withexception=true}" })
{
config.AddRule(LogLevel.Debug, LogLevel.Fatal, new AsyncTargetWrapper(logfile));
}
LogManager.Configuration = config;
}
public static void Info(string msg, params object[] args)
{
logger.Info(msg, args);
}
public static void Debug(string msg, params object[] args)
{
logger.Debug(msg, args);
}
public static void Error(string msg, Exception ex = null)
{
if (ex != null && !errorCache.ContainsKey(ex.Message + ex.StackTrace))
{
errorCache[ex.Message + ex.StackTrace] = msg;
logger.Error(ex, msg);
}
}
public static void Fatal(string msg, Exception ex = null)
{
if (ex != null && !errorCache.ContainsKey(ex.Message + ex.StackTrace))
{
errorCache[ex.Message + ex.StackTrace] = msg;
logger.Fatal(ex, msg);
}
}
public static void flush()
{
LogManager.Flush();
}
}
}