Dreamine.Logging
1.0.2
Dreamine.Logging 프로젝트의 API와 구성 요소를 제공합니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
TextFileLogSink.cs
이 파일의 문서화 페이지로 가기
1
using
System;
2
using
System.IO;
3
using
System.Text;
4
using
Dreamine.Logging.Interfaces
;
5
using
Dreamine.Logging.Models
;
6
7
namespace
Dreamine.Logging.Sinks
8
{
25
public
sealed
class
TextFileLogSink
:
IDreamineLogSink
, IDisposable
26
{
35
private
const
int
DefaultFlushEveryWriteCount
= 20;
36
45
private
readonly
object
_syncRoot
=
new
();
54
private
readonly
string
_logDirectory
;
63
private
readonly
IDreamineLogFormatter
_formatter
;
72
private
readonly
int
_flushEveryWriteCount
;
81
private
StreamWriter?
_writer
;
90
private
string
?
_currentFilePath
;
99
private
int
_pendingFlushCount
;
108
private
bool
_disposed
;
109
150
public
TextFileLogSink
(
151
string
logDirectory,
152
IDreamineLogFormatter
formatter,
153
int
flushEveryWriteCount =
DefaultFlushEveryWriteCount
)
154
{
155
_logDirectory
=
string
.IsNullOrWhiteSpace(logDirectory)
156
?
"Logs"
157
: logDirectory;
158
159
_formatter
= formatter ??
throw
new
ArgumentNullException(nameof(formatter));
160
_flushEveryWriteCount
= flushEveryWriteCount <= 0
161
?
DefaultFlushEveryWriteCount
162
: flushEveryWriteCount;
163
}
164
189
public
void
Write
(
DreamineLogEntry
entry)
190
{
191
ArgumentNullException.ThrowIfNull(entry);
192
193
lock (
_syncRoot
)
194
{
195
if
(
_disposed
)
196
{
197
return
;
198
}
199
200
var filePath =
GetFilePath
(entry.
Timestamp
);
201
EnsureWriter
(filePath);
202
203
var text =
_formatter
.Format(entry);
204
205
_writer
!.WriteLine(text);
206
_pendingFlushCount
++;
207
208
if
(
_pendingFlushCount
>=
_flushEveryWriteCount
209
|| entry.
Level
>=
DreamineLogLevel
.Error)
210
{
211
FlushCore
();
212
}
213
}
214
}
215
224
public
void
Dispose
()
225
{
226
lock (
_syncRoot
)
227
{
228
if
(
_disposed
)
229
{
230
return
;
231
}
232
233
try
234
{
235
FlushCore
();
236
}
237
catch
238
{
239
// Suppress flush errors during dispose.
240
}
241
242
_writer
?.Dispose();
243
_writer
=
null
;
244
_disposed
=
true
;
245
}
246
}
247
272
private
string
GetFilePath
(DateTimeOffset timestamp)
273
{
274
Directory.CreateDirectory(
_logDirectory
);
275
return
Path.Combine(
_logDirectory
, $
"{timestamp:yyyy-MM-dd}.log"
);
276
}
277
294
private
void
EnsureWriter
(
string
filePath)
295
{
296
if
(
_writer
is not
null
297
&&
string
.Equals(
_currentFilePath
, filePath, StringComparison.OrdinalIgnoreCase))
298
{
299
return
;
300
}
301
302
// Date rolled over (or first write) — close the previous file and open the new one.
303
FlushCore
();
304
_writer
?.Dispose();
305
306
_currentFilePath
= filePath;
307
_writer
=
new
StreamWriter(
308
new
FileStream(
309
filePath,
310
FileMode.Append,
311
FileAccess.Write,
312
FileShare.ReadWrite),
313
Encoding.UTF8)
314
{
315
AutoFlush =
false
316
};
317
}
318
327
private
void
FlushCore
()
328
{
329
if
(
_writer
is
null
)
330
{
331
_pendingFlushCount
= 0;
332
return
;
333
}
334
335
_writer
.Flush();
336
_pendingFlushCount
= 0;
337
}
338
}
339
}
Dreamine.Logging.Interfaces
Definition
IDreamineLogFormatter.cs:3
Dreamine.Logging.Models
Definition
DreamineLogEntry.cs:3
Dreamine.Logging.Models.DreamineLogLevel
DreamineLogLevel
Definition
DreamineLogLevel.cs:12
Dreamine.Logging.Sinks
Definition
AsyncQueueSink.cs:10
Dreamine.Logging.Interfaces.IDreamineLogFormatter
Definition
IDreamineLogFormatter.cs:14
Dreamine.Logging.Interfaces.IDreamineLogSink
Definition
IDreamineLogSink.cs:14
Dreamine.Logging.Models.DreamineLogEntry
Definition
DreamineLogEntry.cs:14
Dreamine.Logging.Models.DreamineLogEntry.Timestamp
DateTimeOffset Timestamp
Definition
DreamineLogEntry.cs:23
Dreamine.Logging.Models.DreamineLogEntry.Level
DreamineLogLevel Level
Definition
DreamineLogEntry.cs:33
Dreamine.Logging.Sinks.TextFileLogSink._currentFilePath
string? _currentFilePath
Definition
TextFileLogSink.cs:90
Dreamine.Logging.Sinks.TextFileLogSink._writer
StreamWriter? _writer
Definition
TextFileLogSink.cs:81
Dreamine.Logging.Sinks.TextFileLogSink.Write
void Write(DreamineLogEntry entry)
Definition
TextFileLogSink.cs:189
Dreamine.Logging.Sinks.TextFileLogSink._logDirectory
readonly string _logDirectory
Definition
TextFileLogSink.cs:54
Dreamine.Logging.Sinks.TextFileLogSink._syncRoot
readonly object _syncRoot
Definition
TextFileLogSink.cs:45
Dreamine.Logging.Sinks.TextFileLogSink.FlushCore
void FlushCore()
Definition
TextFileLogSink.cs:327
Dreamine.Logging.Sinks.TextFileLogSink._flushEveryWriteCount
readonly int _flushEveryWriteCount
Definition
TextFileLogSink.cs:72
Dreamine.Logging.Sinks.TextFileLogSink.TextFileLogSink
TextFileLogSink(string logDirectory, IDreamineLogFormatter formatter, int flushEveryWriteCount=DefaultFlushEveryWriteCount)
Definition
TextFileLogSink.cs:150
Dreamine.Logging.Sinks.TextFileLogSink.DefaultFlushEveryWriteCount
const int DefaultFlushEveryWriteCount
Definition
TextFileLogSink.cs:35
Dreamine.Logging.Sinks.TextFileLogSink._pendingFlushCount
int _pendingFlushCount
Definition
TextFileLogSink.cs:99
Dreamine.Logging.Sinks.TextFileLogSink._disposed
bool _disposed
Definition
TextFileLogSink.cs:108
Dreamine.Logging.Sinks.TextFileLogSink.EnsureWriter
void EnsureWriter(string filePath)
Definition
TextFileLogSink.cs:294
Dreamine.Logging.Sinks.TextFileLogSink.GetFilePath
string GetFilePath(DateTimeOffset timestamp)
Definition
TextFileLogSink.cs:272
Dreamine.Logging.Sinks.TextFileLogSink._formatter
readonly IDreamineLogFormatter _formatter
Definition
TextFileLogSink.cs:63
Dreamine.Logging.Sinks.TextFileLogSink.Dispose
void Dispose()
Definition
TextFileLogSink.cs:224
Sinks
TextFileLogSink.cs
다음에 의해 생성됨 :
1.17.0