forked from winsw/winsw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogAppenders.cs
314 lines (276 loc) · 11.6 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
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace winsw
{
// ReSharper disable once InconsistentNaming
public interface EventLogger
{
void LogEvent(string message);
void LogEvent(string message, EventLogEntryType type);
}
/// <summary>
/// Abstraction for handling log.
/// </summary>
public abstract class LogHandler
{
// ReSharper disable once InconsistentNaming
public abstract void log(Stream outputStream, Stream errorStream);
/// <summary>
/// Error and information about logging should be reported here.
/// </summary>
public EventLogger EventLogger { set; get; }
/// <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);
}
}
}
/// <summary>
/// Base class for file-based loggers
/// </summary>
public abstract class AbstractFileLogAppender : LogHandler
{
protected string BaseLogFileName { private set; get; }
public AbstractFileLogAppender(string logDirectory, string baseName)
{
BaseLogFileName = Path.Combine(logDirectory, baseName);
}
}
public abstract class SimpleLogAppender : AbstractFileLogAppender
{
public FileMode FileMode { private set; get; }
public string OutputLogFileName { private set; get; }
public string ErrorLogFileName { private set; get; }
public SimpleLogAppender(string logDirectory, string baseName, FileMode fileMode)
: base(logDirectory, baseName)
{
FileMode = fileMode;
OutputLogFileName = BaseLogFileName + ".out.log";
ErrorLogFileName = BaseLogFileName + ".err.log";
}
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)
{
}
}
/// <summary>
/// LogHandler that throws away output
/// </summary>
public class IgnoreLogAppender : LogHandler
{
public override void log(Stream outputStream, Stream errorStream)
{
new Thread(delegate() { CopyStream(outputStream, Stream.Null); }).Start();
new Thread(delegate() { CopyStream(errorStream, Stream.Null); }).Start();
}
}
public class TimeBasedRollingLogAppender : AbstractFileLogAppender
{
public string Pattern { get; private set; }
public int Period { get; private set; }
public TimeBasedRollingLogAppender(string logDirectory, string baseName, string pattern, int period)
: base(logDirectory, baseName)
{
Pattern = pattern;
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.Append);
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 and write everything to the new file.
w = new FileStream(BaseLogFileName + "_" + periodicRollingCalendar.format + ext, FileMode.Create);
rolled = true;
if (offset < len)
{
w.Write(buf, offset, len - offset);
break;
}
}
}
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 : AbstractFileLogAppender
{
// ReSharper disable once InconsistentNaming
public static int BYTES_PER_KB = 1024;
// ReSharper disable once InconsistentNaming
public static int BYTES_PER_MB = 1024 * BYTES_PER_KB;
// ReSharper disable once InconsistentNaming
public static int DEFAULT_SIZE_THRESHOLD = 10 * BYTES_PER_MB; // rotate every 10MB.
// ReSharper disable once InconsistentNaming
public static int DEFAULT_FILES_TO_KEEP = 8;
public int SizeTheshold { private set; get; }
public int FilesToKeep { private set; get; }
public SizeBasedRollingLogAppender(string logDirectory, string baseName, int sizeThreshold, int filesToKeep)
: base(logDirectory, baseName)
{
SizeTheshold = sizeThreshold;
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 < SizeTheshold)
{// 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 < SizeTheshold) 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);
}
}
}