-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
StringBuilderLog.cs
180 lines (152 loc) · 4.44 KB
/
StringBuilderLog.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
174
175
176
177
178
179
180
using System;
using System.Text;
namespace ServiceStack.Logging
{
/// <summary>
/// StringBuilderLog writes to shared StringBuffer.
/// Made public so its testable
/// </summary>
public class StringBuilderLogFactory : ILogFactory
{
private readonly StringBuilder sb;
private readonly bool debugEnabled;
public StringBuilderLogFactory(bool debugEnabled = true)
{
sb = new StringBuilder();
this.debugEnabled = debugEnabled;
}
public ILog GetLogger(Type type)
{
return new StringBuilderLog(type, sb) { IsDebugEnabled = debugEnabled };
}
public ILog GetLogger(string typeName)
{
return new StringBuilderLog(typeName, sb) { IsDebugEnabled = debugEnabled };
}
public string GetLogs()
{
lock (sb)
return sb.ToString();
}
public void ClearLogs()
{
lock (sb)
sb.Remove(0, sb.Length - 1);
}
}
public class StringBuilderLog : ILog
{
const string DEBUG = "DEBUG: ";
const string ERROR = "ERROR: ";
const string FATAL = "FATAL: ";
const string INFO = "INFO: ";
const string WARN = "WARN: ";
private readonly StringBuilder logs;
public StringBuilderLog(string type, StringBuilder logs)
{
this.logs = logs;
}
public StringBuilderLog(Type type, StringBuilder logs)
{
this.logs = logs;
}
public bool IsDebugEnabled { get; set; }
/// <summary>
/// Logs the specified message.
/// </summary>
private void Log(object message, Exception exception)
{
var msg = message?.ToString() ?? string.Empty;
if (exception != null)
{
msg += ", Exception: " + exception.Message;
}
lock (logs)
logs.AppendLine(msg);
}
/// <summary>
/// Logs the format.
/// </summary>
private void LogFormat(object message, params object[] args)
{
var msg = message?.ToString() ?? string.Empty;
lock (logs)
{
logs.AppendFormat(msg, args);
logs.AppendLine();
}
}
/// <summary>
/// Logs the specified message.
/// </summary>
/// <param name="message">The message.</param>
private void Log(object message)
{
var msg = message?.ToString() ?? string.Empty;
lock (logs)
{
logs.AppendLine(msg);
}
}
public void Debug(object message, Exception exception)
{
Log(DEBUG + message, exception);
}
public void Debug(object message)
{
Log(DEBUG + message);
}
public void DebugFormat(string format, params object[] args)
{
LogFormat(DEBUG + format, args);
}
public void Error(object message, Exception exception)
{
Log(ERROR + message, exception);
}
public void Error(object message)
{
Log(ERROR + message);
}
public void ErrorFormat(string format, params object[] args)
{
LogFormat(ERROR + format, args);
}
public void Fatal(object message, Exception exception)
{
Log(FATAL + message, exception);
}
public void Fatal(object message)
{
Log(FATAL + message);
}
public void FatalFormat(string format, params object[] args)
{
LogFormat(FATAL + format, args);
}
public void Info(object message, Exception exception)
{
Log(INFO + message, exception);
}
public void Info(object message)
{
Log(INFO + message);
}
public void InfoFormat(string format, params object[] args)
{
LogFormat(INFO + format, args);
}
public void Warn(object message, Exception exception)
{
Log(WARN + message, exception);
}
public void Warn(object message)
{
Log(WARN + message);
}
public void WarnFormat(string format, params object[] args)
{
LogFormat(WARN + format, args);
}
}
}