This repository was archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFolder.cs
226 lines (198 loc) · 6.57 KB
/
Folder.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
using Microsoft.Web.Administration;
using System;
using System.Diagnostics;
using System.DirectoryServices;
using System.IO;
namespace IisLogRotator
{
[DebuggerDisplay("ID = {ID}, Enabled = {Enabled}, FilenameFormat = {FilenameFormat}, Directory = {Directory}")]
public class Folder
{
private static readonly Guid IisLogModuleId = new Guid("{FF160657-DE82-11CF-BC0A-00AA006111E0}");
private static readonly Guid NcsaLogModuleId = new Guid("{FF16065F-DE82-11CF-BC0A-00AA006111E0}");
private static readonly Guid W3cLogModuleId = new Guid("{FF160663-DE82-11CF-BC0A-00AA006111E0}");
private static readonly Guid OdbcLogModuleId = new Guid("{FF16065B-DE82-11CF-BC0A-00AA006111E0}");
private Folder()
{
}
public string ID { get; private set; }
public bool Enabled { get; private set; }
public bool IsUTF8 { get; private set; }
public string Directory { get; private set; }
public string FilenameFormat { get; private set; }
public string FileExtension { get; private set; }
public IisServiceType IisService { get; private set; }
public IisPeriodType Period { get; private set; }
public IisLogFormatType LogFormat { get; private set; }
public bool HasCustomFields { get; private set; }
public bool IsLocalTimeRollover { get; private set; }
public long TruncateSize { get; private set; }
public static Folder Create(
ConfigurationElement logFileElement,
IisServiceType iisServiceType,
bool isUTF8,
bool isCentralW3C = false,
bool isCentralBinary = false,
long? siteId = null)
{
IisLogFormatType logFormat;
if (isCentralBinary)
logFormat = IisLogFormatType.CentralBinary;
else if (isCentralW3C)
logFormat = IisLogFormatType.CentralW3C;
else if (iisServiceType == IisServiceType.FTPSVC)
logFormat = IisLogFormatType.W3C;
else
logFormat = (IisLogFormatType)logFileElement["logFormat"];
bool hasCustomFields = false;
if (logFormat == IisLogFormatType.W3C || logFormat == IisLogFormatType.CentralW3C)
{
hasCustomFields = logFileElement.GetCollection("customFields")?.Count > 0;
}
return Create(
(bool)logFileElement["enabled"],
(string)logFileElement["directory"],
iisServiceType,
logFormat,
(IisPeriodType)logFileElement["period"],
isUTF8,
(bool)logFileElement["localTimeRollover"],
hasCustomFields,
(long)logFileElement["truncateSize"],
siteId
);
}
public static Folder Create(
DirectoryEntry logFileEntry,
IisServiceType iisServiceType,
bool isUTF8,
bool isCentralW3C = false,
bool isCentralBinary = false,
long? siteId = null)
{
IisLogFormatType logFormat = IisLogFormatType.Custom;
if (isCentralBinary)
{
logFormat = IisLogFormatType.CentralBinary;
}
else if (isCentralW3C)
{
logFormat = IisLogFormatType.CentralW3C;
}
else if (logFileEntry.Properties["LogPluginClsid"].Value != null)
{
Guid logPluginClsid = Guid.Parse((string)logFileEntry.Properties["LogPluginClsid"].Value);
if (logPluginClsid == IisLogModuleId)
logFormat = IisLogFormatType.IIS;
else if (logPluginClsid == NcsaLogModuleId)
logFormat = IisLogFormatType.NCSA;
else if (logPluginClsid == W3cLogModuleId)
logFormat = IisLogFormatType.W3C;
}
bool isLocaltimeRollover = false;
if (logFileEntry.SchemaClassName == "IIsWebService"
|| logFileEntry.SchemaClassName == "IIsWebServer"
|| logFileEntry.SchemaClassName == "IIsFtpService"
|| logFileEntry.SchemaClassName == "IIsFtpServer")
{
isLocaltimeRollover = (bool)logFileEntry.Properties["LogFileLocaltimeRollover"].Value;
}
return Create(
((int)logFileEntry.Properties["LogType"].Value == 1),
(string)logFileEntry.Properties["LogFileDirectory"].Value,
iisServiceType,
logFormat,
(IisPeriodType)logFileEntry.Properties["LogFilePeriod"].Value,
isUTF8,
isLocaltimeRollover,
false,
(int)logFileEntry.Properties["LogFileTruncateSize"].Value,
siteId
);
}
private static Folder Create(
bool enabled,
string dir,
IisServiceType iisService,
IisLogFormatType logFormat,
IisPeriodType period,
bool isUTF8,
bool isLocalTimeRollover,
bool hasCustomFields,
long truncateSize,
long? siteId)
{
string subdir = iisService.ToString("G");
string filePrefix = (logFormat == IisLogFormatType.CentralBinary) ? null : isUTF8 ? "u_" : null;
string fileSuffix = hasCustomFields ? "_x" : null;
string fileExtension = ".log";
if (logFormat != IisLogFormatType.CentralBinary && logFormat != IisLogFormatType.CentralW3C)
{
subdir += siteId.Value.ToString();
}
switch (logFormat)
{
case IisLogFormatType.CentralBinary:
filePrefix = (period == IisPeriodType.MaxSize) ? "raw" : "ra";
fileExtension = ".ibl";
break;
case IisLogFormatType.IIS:
filePrefix += (period == IisPeriodType.MaxSize) ? "inetsv" : "in";
break;
case IisLogFormatType.NCSA:
filePrefix += (period == IisPeriodType.MaxSize) ? "ncsa" : "nc";
break;
case IisLogFormatType.CentralW3C:
case IisLogFormatType.W3C:
filePrefix += (period == IisPeriodType.MaxSize) ? "extend" : "ex";
break;
default:
throw new NotImplementedException("logFormat=" + logFormat + " is not yet implemented");
}
string fileformat;
switch (period)
{
case IisPeriodType.MaxSize: fileformat = "{0}"; break;
case IisPeriodType.Hourly: fileformat = "{0:yyMMddhh}"; break;
case IisPeriodType.Daily: fileformat = "{0:yyMMdd}"; break;
case IisPeriodType.Weekly: fileformat = "{0:yyMM}{1:00}"; break;
case IisPeriodType.Monthly: fileformat = "{0:yyMM}"; break;
default: throw new NotImplementedException("period=" + period + " is not yet implemented");
}
string directory = null;
string fileLogFormat = null;
if (logFormat != IisLogFormatType.Custom)
{
directory = Path.Combine(
Environment.ExpandEnvironmentVariables(dir),
subdir
);
fileLogFormat = string.Concat(
filePrefix,
fileformat,
fileSuffix,
fileExtension
);
}
return new Folder()
{
ID = subdir,
Enabled = enabled,
IsUTF8 = isUTF8,
Directory = directory,
FilenameFormat = fileLogFormat,
FileExtension = fileExtension,
IisService = iisService,
Period = period,
LogFormat = logFormat,
HasCustomFields = hasCustomFields,
IsLocalTimeRollover = isLocalTimeRollover,
TruncateSize = truncateSize
};
}
public override string ToString()
{
return this.Directory;
}
}
}