-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathLogger.cs
More file actions
137 lines (122 loc) · 3.74 KB
/
Copy pathLogger.cs
File metadata and controls
137 lines (122 loc) · 3.74 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BarRaider.SdTools
{
/// <summary>
/// Tracing levels used for Logger
/// </summary>
public enum TracingLevel
{
/// <summary>
/// Debug level
/// </summary>
DEBUG,
/// <summary>
/// Informational level
/// </summary>
INFO,
/// <summary>
/// Warning level
/// </summary>
WARN,
/// <summary>
/// Error level
/// </summary>
ERROR,
/// <summary>
/// Fatal (highest) level
/// </summary>
FATAL
}
/// <summary>
/// Log4Net logger helper class
/// </summary>
public class Logger
{
private static Logger instance = null;
private static readonly object objLock = new object();
/// <summary>
/// Returns singelton entry of Log4Net logger
/// </summary>
public static Logger Instance
{
get
{
if (instance != null)
{
return instance;
}
lock (objLock)
{
if (instance == null)
{
instance = new Logger();
}
return instance;
}
}
}
/// <summary>
/// Set logging format. In addition to the standard layouts defined for NLog, ${real-time} ${elapsed-time} are available.
/// </summary>
/// <example>"https://nlog-project.org/config/?tab=layout-renderers"</example>
public string Layout
{
get
{
LoggingConfiguration lc = NLog.LogManager.Configuration;
return ((FileTarget)lc.LoggingRules[0].Targets[0]).Layout.ToString();
}
set
{
LoggingConfiguration lc = NLog.LogManager.Configuration;
((FileTarget)lc.LoggingRules[0].Targets[0]).Layout = value;
}
}
private readonly NLog.Logger log = null;
private Logger()
{
LayoutRenderer.Register<LayoutRenderers.ElapsedTimeLayoutRenderer>("elapsed-time");
LayoutRenderer.Register<LayoutRenderers.RealTimeLayoutRenderer>("real-time");
var config = new NLog.Config.LoggingConfiguration();
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "pluginlog.log", ArchiveEvery=NLog.Targets.FileArchivePeriod.Day, MaxArchiveFiles=10, ArchiveFileName="logs/log.{###}.log", ArchiveNumbering=NLog.Targets.ArchiveNumberingMode.Rolling};
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
NLog.LogManager.Configuration = config;
log = LogManager.GetCurrentClassLogger();
LogMessage(TracingLevel.DEBUG, "Logger Initialized");
}
/// <summary>
/// Add message to log with a specific severity level.
/// </summary>
/// <param name="Level"></param>
/// <param name="Message"></param>
public void LogMessage(TracingLevel Level, string Message)
{
switch (Level)
{
case TracingLevel.DEBUG:
log.Debug(Message);
break;
case TracingLevel.INFO:
log.Info(Message);
break;
case TracingLevel.WARN:
log.Warn(Message);
break;
case TracingLevel.ERROR:
log.Error(Message);
break;
case TracingLevel.FATAL:
log.Fatal(Message);
break;
}
}
}
}