forked from r4dius/Iso2God
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRollingFileAppender.cs
138 lines (124 loc) · 3.06 KB
/
RollingFileAppender.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
using System;
using System.IO;
using System.Text;
namespace EnterpriseDT.Util.Debug;
public class RollingFileAppender : FileAppender
{
private const long DEFAULT_MAXSIZE = 10485760L;
private const int CHECK_COUNT_FREQUENCY = 100;
private long maxFileSize = 10485760L;
private int sizeCheckCount = 0;
private int maxSizeRollBackups = 1;
public int MaxSizeRollBackups
{
get
{
return maxSizeRollBackups;
}
set
{
maxSizeRollBackups = ((value >= 0) ? value : 0);
}
}
public long MaxFileSize
{
get
{
return maxFileSize;
}
set
{
maxFileSize = value;
}
}
public RollingFileAppender(string fileName, long maxFileSize)
: base(fileName)
{
this.maxFileSize = maxFileSize;
}
public RollingFileAppender(string fileName)
: base(fileName)
{
}
private void CheckForRollover()
{
try
{
long num = fileStream.Position;
if (sizeCheckCount >= 100)
{
num = fileStream.Length;
sizeCheckCount = 0;
}
else
{
sizeCheckCount++;
}
if (num > maxFileSize)
{
Rollover();
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to rollover log files (" + ex.Message + ")");
}
}
private void Rollover()
{
Close();
FileInfo fileInfo = new FileInfo(base.FileName);
if (maxSizeRollBackups == 0)
{
fileInfo.Delete();
}
else
{
FileInfo fileInfo2 = new FileInfo(base.FileName + "." + maxSizeRollBackups);
if (fileInfo2.Exists)
{
fileInfo2.Delete();
}
for (int num = maxSizeRollBackups - 1; num > 0; num--)
{
fileInfo2 = new FileInfo(base.FileName + "." + num);
if (fileInfo2.Exists)
{
fileInfo2.MoveTo(base.FileName + "." + (num + 1));
}
}
fileInfo.MoveTo(base.FileName + ".1");
}
sizeCheckCount = 0;
Open();
}
public override void Log(string msg)
{
if (!closed)
{
CheckForRollover();
logger.WriteLine(msg);
logger.Flush();
}
else
{
Console.WriteLine(msg);
}
}
public override void Log(Exception t)
{
StringBuilder stringBuilder = new StringBuilder(((object)t).GetType().FullName);
stringBuilder.Append(": ").Append(t.Message);
if (!closed)
{
CheckForRollover();
logger.WriteLine(stringBuilder.ToString());
logger.WriteLine(t.StackTrace.ToString());
logger.Flush();
}
else
{
Console.WriteLine(stringBuilder.ToString());
}
}
}