/
DotNetLogging.cs
344 lines (308 loc) · 9.94 KB
/
DotNetLogging.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System.Diagnostics;
using System.Reflection;
using Microsoft.Extensions.Logging;
using NReco.Logging.File;
namespace LoggingNReco_DotNetLogging;
/// <summary>
/// 공유 로거Shared logger
/// </summary>
/// <remarks>
/// https://stackoverflow.com/questions/48676152/asp-net-core-web-api-logging-from-a-static-class
/// </remarks>
public class DotNetLogging
{
/// <summary>
/// 로거 팩토리
/// </summary>
public ILoggerFactory LoggerFactory_My { get; set; }
#pragma warning disable CS8618
/// <summary>
/// 기본 생성
/// <para>로거를 초기화를 하지 않으므로 임시 생성일때만 사용한다.</para>
/// </summary>
public DotNetLogging()
{
this.LoggerFactory_My = new LoggerFactory();
}
/// <summary>
/// 로거는 자동 생성하고 로거를 기본 옵션으로 초기화 한다.
/// </summary>
/// <param name="sPathFormat">로그 파일을 생성할 경로 포맷</param>
/// <param name="bConsole">콘솔 표시 여부</param>
public DotNetLogging(
string? sPathFormat
, bool bConsole)
{
this.Initialize(null, sPathFormat, bConsole);
}
/// <summary>
/// 전달받은 ILoggerFactory개체를 이용하여 로거를 초기화한다.
/// <para>null이면 기본 옵션으로 새로 생성한다.</para>
/// </summary>
/// <remarks>
/// 자동 생성시 NReco.Logging.File를 기준으로 작성된다.
/// </remarks>
/// <param name="loggerFactory">생성한 ILoggerFactory 개채. null이면 자동생성</param>
/// <param name="sPathFormat">로그 파일을 생성할 경로 포맷</param>
/// <param name="bConsole">콘솔 표시 여부</param>
public DotNetLogging(
ILoggerFactory? loggerFactory
, string? sPathFormat
, bool bConsole)
{
this.Initialize(loggerFactory, sPathFormat, bConsole);
}
#pragma warning restore CS8618
/// <summary>
/// 이 개체를 초기화 한다.
/// </summary>
/// <remarks>
/// 사용하는 프로젝트가 Microsoft.Extensions.Logging를 참조하고 있어야 한다.
/// (누겟에서 설치 가능)
/// </remarks>
/// <param name="loggerFactory"></param>
/// <param name="sPathFormat"></param>
/// <param name="bConsole"></param>
public void Initialize(
ILoggerFactory? loggerFactory
, string? sPathFormat
, bool bConsole)
{
if (null != loggerFactory)
{
this.LoggerFactory_My = loggerFactory;
}
else
{
if (null == sPathFormat)
{
//https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#non-host-console-app
this.LoggerFactory_My
= LoggerFactory.Create(
loggingBuilder => DotNetLogging.configure(loggingBuilder, bConsole));
}
else
{
this.LoggerFactory_My
= LoggerFactory.Create(
loggingBuilder => DotNetLogging.configure(loggingBuilder, sPathFormat, bConsole));
}
}
}
/// <summary>
/// 미리 세팅된 기본 옵션으로 로거를 생성한다.
/// <para>파일 경로도 기본 세팅된 것을 기준으로 사용한다.</para>
/// </summary>
/// <param name="loggingBuilder"></param>
/// <param name="bConsole">콘솔 표시 여부</param>
/// <returns></returns>
public static ILoggingBuilder configure(
ILoggingBuilder loggingBuilder
, bool bConsole)
{
return configure(
loggingBuilder
, Path.Combine("Logs", "Log_{0:yyyy}-{0:MM}-{0:dd}.log")
, bConsole);
}
/// <summary>
/// 미리 세팅된 기본 옵션으로 로거를 생성한다.
/// </summary>
/// <param name="loggingBuilder"></param>
/// <param name="sPathFormat">로그 파일을 생성할 경로 포맷</param>
/// <param name="bConsole">콘솔 표시 여부</param>
/// <returns></returns>
public static ILoggingBuilder configure(
ILoggingBuilder loggingBuilder
, string sPathFormat
, bool bConsole)
{
if (true == bConsole)
{//콘솔 사용
loggingBuilder.AddSimpleConsole(x => x.TimestampFormat = "[yyyy-MM-dd HH:mm:ss] ");
//loggingBuilder.AddSimpleConsole(x=>x.TimestampFormat)
}
//로거 표시 설정(디버거 등의 메시지 출력 설정)
loggingBuilder.AddFilter(
(provider, category, logLevel) =>
{
return true;
});
loggingBuilder.AddFile(sPathFormat
, fileLoggerOpts =>
{
fileLoggerOpts.FormatLogFileName = sNameFormat =>
{
return String.Format(sNameFormat, DateTime.Now);
};
fileLoggerOpts.FormatLogEntry = (lmMsg) =>
{
//string sLevel = string.Empty;
//switch (lmMsg.LogLevel)
//{
// case LogLevel.Information:
// sLevel = "Info";
// break;
// case LogLevel.Warning:
// sLevel = "Warn";
// break;
// default:
// sLevel = lmMsg.LogLevel.ToString();
// break;
//}
return $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] {lmMsg.LogLevel} [{lmMsg.LogName}] {lmMsg.Message}";
};
});
return loggingBuilder;
}
/// <summary>
/// 개체를 전달하여 로거 생성
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public ILogger CreateLogger<T>() => LoggerFactory_My.CreateLogger<T>();
/// <summary>
/// 카테고리이름을 직접 입력하여 로거 생성
/// </summary>
/// <param name="sCategoryName"></param>
/// <returns></returns>
public ILogger CreateLogger(string sCategoryName)
=> LoggerFactory_My.CreateLogger(sCategoryName);
/// <summary>
/// 호출한 클래스를 찾아 카테고리 이름으로 출력한다.
/// </summary>
/// <returns>없으면 빈값</returns>
private string CategoryName()
{
string sReturn = string.Empty;
//= new StackTrace().GetFrame(2).GetMethod().ReflectedType.Name;
StackTrace stTemp = new StackTrace();
StackFrame? sfTemp = stTemp.GetFrame(2);
if (null != sfTemp)
{
MethodBase? mbTemp = sfTemp.GetMethod();
if (null != mbTemp)
{
Type? typeTemp = mbTemp.ReflectedType;
if (null != mbTemp)
{
sReturn = mbTemp.Name;
}
}
}
return sReturn;
}
/// <summary>
/// 정보 로그로 출력
/// </summary>
/// <param name="sMessage"></param>
public void LogInfo(string sMessage)
{
this.LogInfo(this.CategoryName(), sMessage);
}
/// <summary>
/// 정보 로그로 출력
/// </summary>
/// <param name="sCategoryName"></param>
/// <param name="sMessage"></param>
public void LogInfo(
string sCategoryName
, string sMessage)
{
this.LoggerFactory_My
.CreateLogger(sCategoryName)
.LogInformation(sMessage);
}
/// <summary>
/// 에러 로그로 출력
/// </summary>
/// <param name="ex"></param>
public void LogError(Exception ex)
{
this.LogError(ex.ToString());
}
/// <summary>
/// 에러 로그로 출력
/// </summary>
/// <param name="sMessage"></param>
public void LogError(string sMessage)
{
this.LogError(this.CategoryName(), sMessage);
}
/// <summary>
/// 에러 로그로 출력
/// </summary>
/// <param name="sCategoryName"></param>
/// <param name="sMessage"></param>
public void LogError(
string sCategoryName
, string sMessage)
{
this.LoggerFactory_My
.CreateLogger(sCategoryName)
.LogError(sMessage);
}
/// <summary>
/// 디버그 로그 출력
/// </summary>
/// <param name="sMessage"></param>
public void LogDebug(string sMessage)
{
this.LogDebug(this.CategoryName(), sMessage);
}
/// <summary>
/// 디버그 로그 출력
/// </summary>
/// <param name="sCategoryName"></param>
/// <param name="sMessage"></param>
public void LogDebug(
string sCategoryName
, string sMessage)
{
this.LoggerFactory_My
.CreateLogger(sCategoryName)
.LogDebug(sMessage);
}
/// <summary>
/// 경고 로그 출력
/// </summary>
/// <param name="sMessage"></param>
public void LogWarning(string sMessage)
{
this.LogWarning(this.CategoryName(), sMessage);
}
/// <summary>
/// 경고 로그 출력
/// </summary>
/// <param name="sCategoryName"></param>
/// <param name="sMessage"></param>
public void LogWarning(
string sCategoryName
, string sMessage)
{
this.LoggerFactory_My
.CreateLogger(sCategoryName)
.LogWarning(sMessage);
}
/// <summary>
/// 심각 로그 출력
/// </summary>
/// <param name="sMessage"></param>
public void LogCritical(string sMessage)
{
this.LogCritical(this.CategoryName(), sMessage);
}
/// <summary>
/// 심각 로그 출력
/// </summary>
/// <param name="sCategoryName"></param>
/// <param name="sMessage"></param>
public void LogCritical(
string sCategoryName
, string sMessage)
{
this.LoggerFactory_My
.CreateLogger(sCategoryName)
.LogCritical(sMessage);
}
}