/
LogAppenders.cs
319 lines (277 loc) · 10.8 KB
/
LogAppenders.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace winsw
{
public interface EventLogger
{
void LogEvent(string message);
void LogEvent(string message, EventLogEntryType type);
}
/// <summary>
/// Abstraction for handling log.
/// </summary>
public abstract class LogHandler
{
private EventLogger eventLogger;
private string baseLogFileName;
public LogHandler(string logDirectory, string baseName)
{
this.baseLogFileName = Path.Combine(logDirectory, baseName);
}
public abstract void log(Stream outputStream, Stream errorStream);
public EventLogger EventLogger
{
set
{
this.eventLogger = value;
}
get
{
return this.eventLogger;
}
}
public string BaseLogFileName
{
get
{
return this.baseLogFileName;
}
}
/// <summary>
/// Convenience method to copy stuff from StreamReader to StreamWriter
/// </summary>
protected void CopyStream(Stream i, Stream o)
{
byte[] buf = new byte[1024];
while (true)
{
int sz = i.Read(buf, 0, buf.Length);
if (sz == 0) break;
o.Write(buf, 0, sz);
o.Flush();
}
i.Close();
o.Close();
}
/// <summary>
/// File replacement.
/// </summary>
protected void CopyFile(string sourceFileName, string destFileName)
{
try
{
File.Delete(destFileName);
File.Move(sourceFileName, destFileName);
}
catch (IOException e)
{
EventLogger.LogEvent("Failed to copy :" + sourceFileName + " to " + destFileName + " because " + e.Message);
}
}
}
public abstract class SimpleLogAppender : LogHandler
{
private FileMode fileMode;
private string outputLogFileName;
private string errorLogFileName;
public SimpleLogAppender(string logDirectory, string baseName, FileMode fileMode)
: base(logDirectory, baseName)
{
this.fileMode = fileMode;
this.outputLogFileName = BaseLogFileName + ".out.log";
this.errorLogFileName = BaseLogFileName + ".err.log";
}
public string OutputLogFileName
{
get
{
return this.outputLogFileName;
}
}
public string ErrorLogFileName
{
get
{
return this.errorLogFileName;
}
}
public override void log(Stream outputStream, Stream errorStream)
{
new Thread(delegate() { CopyStream(outputStream, new FileStream(outputLogFileName, fileMode)); }).Start();
new Thread(delegate() { CopyStream(errorStream, new FileStream(errorLogFileName, fileMode)); }).Start();
}
}
public class DefaultLogAppender : SimpleLogAppender
{
public DefaultLogAppender(string logDirectory, string baseName)
: base(logDirectory, baseName, FileMode.Append)
{
}
}
public class ResetLogAppender : SimpleLogAppender
{
public ResetLogAppender(string logDirectory, string baseName)
: base(logDirectory, baseName, FileMode.Create)
{
}
}
public class TimeBasedRollingLogAppender : LogHandler
{
private string pattern;
private int period;
public TimeBasedRollingLogAppender(string logDirectory, string baseName, string pattern, int period)
: base(logDirectory, baseName)
{
this.pattern = pattern;
this.period = period;
}
public override void log(Stream outputStream, Stream errorStream)
{
new Thread(delegate() { CopyStreamWithDateRotation(outputStream, ".out.log"); }).Start();
new Thread(delegate() { CopyStreamWithDateRotation(errorStream, ".err.log"); }).Start();
}
/// <summary>
/// Works like the CopyStream method but does a log rotation based on time.
/// </summary>
private void CopyStreamWithDateRotation(Stream data, string ext)
{
PeriodicRollingCalendar periodicRollingCalendar = new PeriodicRollingCalendar(pattern, period);
periodicRollingCalendar.init();
byte[] buf = new byte[1024];
FileStream w = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
while (true)
{
int len = data.Read(buf, 0, buf.Length);
if (len == 0) break; // EOF
if (periodicRollingCalendar.shouldRoll)
{// rotate at the line boundary
int offset = 0;
bool rolled = false;
for (int i = 0; i < len; i++)
{
if (buf[i] == 0x0A)
{// at the line boundary.
// time to rotate.
w.Write(buf, offset, i + 1);
w.Close();
offset = i + 1;
// create a new file.
w = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
rolled = true;
}
}
if (!rolled)
{// we didn't roll - most likely as we didnt find a line boundary, so we should log what we read and roll anyway.
w.Write(buf, 0, len);
w.Close();
w = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
}
}
else
{// typical case. write the whole thing into the current file
w.Write(buf, 0, len);
}
w.Flush();
}
data.Close();
w.Close();
}
}
public class SizeBasedRollingLogAppender : LogHandler
{
public static int BYTES_PER_KB = 1024;
public static int BYTES_PER_MB = 1024 * BYTES_PER_KB;
public static int DEFAULT_SIZE_THRESHOLD = 10 * BYTES_PER_MB; // rotate every 10MB.
public static int DEFAULT_FILES_TO_KEEP = 8;
private int sizeThreshold;
private int filesToKeep;
public SizeBasedRollingLogAppender(string logDirectory, string baseName, int sizeThreshold, int filesToKeep)
: base(logDirectory, baseName)
{
this.sizeThreshold = sizeThreshold;
this.filesToKeep = filesToKeep;
}
public SizeBasedRollingLogAppender(string logDirectory, string baseName)
: this(logDirectory, baseName, DEFAULT_SIZE_THRESHOLD, DEFAULT_FILES_TO_KEEP) { }
public override void log(Stream outputStream, Stream errorStream)
{
new Thread(delegate() { CopyStreamWithRotation(outputStream, ".out.log"); }).Start();
new Thread(delegate() { CopyStreamWithRotation(errorStream, ".err.log"); }).Start();
}
/// <summary>
/// Works like the CopyStream method but does a log rotation.
/// </summary>
private void CopyStreamWithRotation(Stream data, string ext)
{
byte[] buf = new byte[1024];
FileStream w = new FileStream(BaseLogFileName + ext, FileMode.Append);
long sz = new FileInfo(BaseLogFileName + ext).Length;
while (true)
{
int len = data.Read(buf, 0, buf.Length);
if (len == 0) break; // EOF
if (sz + len < sizeThreshold)
{// typical case. write the whole thing into the current file
w.Write(buf, 0, len);
sz += len;
}
else
{
// rotate at the line boundary
int s = 0;
for (int i = 0; i < len; i++)
{
if (buf[i] != 0x0A) continue;
if (sz + i < sizeThreshold) continue;
// at the line boundary and exceeded the rotation unit.
// time to rotate.
w.Write(buf, s, i + 1);
w.Close();
s = i + 1;
try
{
for (int j = filesToKeep; j >= 1; j--)
{
string dst = BaseLogFileName + "." + (j - 1) + ext;
string src = BaseLogFileName + "." + (j - 2) + ext;
if (File.Exists(dst))
File.Delete(dst);
if (File.Exists(src))
File.Move(src, dst);
}
File.Move(BaseLogFileName + ext, BaseLogFileName + ".0" + ext);
}
catch (IOException e)
{
EventLogger.LogEvent("Failed to rotate log: " + e.Message);
}
// even if the log rotation fails, create a new one, or else
// we'll infinitely try to rotate.
w = new FileStream(BaseLogFileName + ext, FileMode.Create);
sz = new FileInfo(BaseLogFileName + ext).Length;
}
}
w.Flush();
}
data.Close();
w.Close();
}
}
/// <summary>
/// Rotate log when a service is newly started.
/// </summary>
public class RollingLogAppender : SimpleLogAppender
{
public RollingLogAppender(string logDirectory, string baseName)
: base(logDirectory, baseName, FileMode.Append)
{
}
public override void log(Stream outputStream, Stream errorStream)
{
CopyFile(OutputLogFileName, OutputLogFileName + ".old");
CopyFile(ErrorLogFileName, ErrorLogFileName + ".old");
base.log(outputStream, errorStream);
}
}
}