-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.cs
173 lines (157 loc) · 5.77 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace SharpMCL
{
public class Logger
{
[Flags]
public enum Targets
{
Console,
File
}
public enum Level
{
Debug,
Info,
Warn,
Error,
Fatal
}
private class Entry
{
private DateTime dateTime;
private Level level;
private int threadId;
private string message;
public Entry(DateTime dateTime, Level level, int threadId, string message)
{
this.dateTime = dateTime;
this.level = level;
this.threadId = threadId;
this.message = message.Replace("\t", "|").Replace("\r", "|").Replace("\n", "|");
}
public DateTime DateTime { get { return dateTime; } }
public Level Level { get { return level; } }
public int ThreadId { get { return threadId; } }
public string Message { get { return message; } }
public void WriteToConsole()
{
var dateTime2 = dateTime.ToString("yyyy.MM.dd HH:mm:ss.fff");
var line = string.Format("{0}\t{1}\t{2}\t{3}", dateTime2, level, threadId, message);
Console.WriteLine(line);
}
public void WriteToFile(string loggerId)
{
var date = dateTime.ToString("yyyyMMdd");
var logFileName = string.Format(string.IsNullOrEmpty(loggerId) ? "{0}.log" : "{0}_{1}.log", date, loggerId);
using (var streamWriter = new StreamWriter(logFileName, true))
{
var time = dateTime.ToString("HH:mm:ss.fff");
var line = string.Format("{0}\t{1}\t{2}\t{3}", time, level, threadId, message);
streamWriter.WriteLine(line);
}
}
}
public const Logger Null = null;
private string id;
private Targets targets;
private object lockObject;
private Queue<Entry> entries;
private Thread thread;
private bool isRunning;
private void Run()
{
while (true)
{
lock (lockObject)
{
if (entries.Count == 0)
{
if (!isRunning)
break;
}
else
{
try
{
var entry = entries.Dequeue();
if (targets.HasFlag(Targets.Console))
entry.WriteToConsole();
if (targets.HasFlag(Targets.File))
entry.WriteToFile(id);
}
catch (Exception exception)
{
Console.WriteLine(exception);
isRunning = false;
}
}
}
Thread.Sleep(10);
}
}
public Logger(string id, Targets targets)
{
this.id = id;
this.targets = targets;
lockObject = new object();
entries = new Queue<Entry>();
thread = null;
isRunning = false;
}
public void Start()
{
lock (lockObject)
{
if (isRunning)
return;
isRunning = true;
}
thread = new Thread(Run);
thread.Start();
}
public void Stop()
{
lock (lockObject)
{
if (!isRunning)
return;
isRunning = false;
}
thread.Join();
thread = null;
}
public void Log(Level level, string message)
{
lock (lockObject)
{
if (!isRunning)
return;
var timeStamp = DateTime.Now;
var threadId = Thread.CurrentThread.ManagedThreadId;
var entry = new Entry(timeStamp, level, threadId, message);
entries.Enqueue(entry);
}
}
public void Debug(string message) { Log(Level.Debug, message); }
public void Info(string message) { Log(Level.Info, message); }
public void Warn(string message) { Log(Level.Warn, message); }
public void Error(string message) { Log(Level.Error, message); }
public void Fatal(string message) { Log(Level.Fatal, message); }
public void Log(Level level, string format, params object[] args) { Log(level, string.Format(format, args)); }
public void Debug(string format, params object[] args) { Log(Level.Debug, format, args); }
public void Info(string format, params object[] args) { Log(Level.Info, format, args); }
public void Warn(string format, params object[] args) { Log(Level.Warn, format, args); }
public void Error(string format, params object[] args) { Log(Level.Error, format, args); }
public void Fatal(string format, params object[] args) { Log(Level.Fatal, format, args); }
public void Log(Level level, object obj) { Log(level, obj.ToString()); }
public void Debug(object obj) { Log(Level.Debug, obj); }
public void Info(object obj) { Log(Level.Info, obj); }
public void Warn(object obj) { Log(Level.Warn, obj); }
public void Error(object obj) { Log(Level.Error, obj); }
public void Fatal(object obj) { Log(Level.Fatal, obj); }
}
}