Updated
This commit is contained in:
@@ -26,9 +26,16 @@ public static class FileLoggerFactoryExtensions
|
||||
public static ILoggingBuilder AddEonaCatFileLogger(this ILoggingBuilder builder, string filenamePrefix = null,
|
||||
FileLoggerOptions fileLoggerOptions = null)
|
||||
{
|
||||
if (fileLoggerOptions == null) fileLoggerOptions = new FileLoggerOptions();
|
||||
if (fileLoggerOptions == null)
|
||||
{
|
||||
fileLoggerOptions = new FileLoggerOptions();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filenamePrefix))
|
||||
{
|
||||
fileLoggerOptions.FileNamePrefix = filenamePrefix;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(filenamePrefix)) fileLoggerOptions.FileNamePrefix = filenamePrefix;
|
||||
builder.AddEonaCatFileLogger(options =>
|
||||
{
|
||||
options.FileNamePrefix = fileLoggerOptions.FileNamePrefix;
|
||||
@@ -55,7 +62,11 @@ public static class FileLoggerFactoryExtensions
|
||||
public static ILoggingBuilder AddEonaCatFileLogger(this ILoggingBuilder builder,
|
||||
Action<FileLoggerOptions> configure)
|
||||
{
|
||||
if (configure == null) throw new ArgumentNullException(nameof(configure));
|
||||
if (configure == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configure));
|
||||
}
|
||||
|
||||
builder.AddEonaCatFileLogger();
|
||||
builder.Services.Configure(configure);
|
||||
|
||||
|
||||
@@ -30,7 +30,10 @@ public class FileLoggerOptions : BatchingLoggerOptions
|
||||
set
|
||||
{
|
||||
if (value <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(FileSizeLimit)} must be positive.");
|
||||
}
|
||||
|
||||
_fileSizeLimit = value;
|
||||
}
|
||||
}
|
||||
@@ -46,8 +49,11 @@ public class FileLoggerOptions : BatchingLoggerOptions
|
||||
set
|
||||
{
|
||||
if (value <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(value),
|
||||
$"{nameof(RetainedFileCountLimit)} must be positive.");
|
||||
}
|
||||
|
||||
_retainedFileCountLimit = value;
|
||||
}
|
||||
}
|
||||
@@ -74,7 +80,10 @@ public class FileLoggerOptions : BatchingLoggerOptions
|
||||
set
|
||||
{
|
||||
if (value <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(MaxRolloverFiles)} must be positive.");
|
||||
}
|
||||
|
||||
_maxRolloverFiles = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,8 +63,12 @@ public class FileLoggerProvider : BatchingLoggerProvider
|
||||
{
|
||||
var dir = Path.GetDirectoryName(_logFile);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,7 +132,10 @@ public class FileLoggerProvider : BatchingLoggerProvider
|
||||
|
||||
private async Task<bool> TryWriteToFileAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!_buffer.ContainsKey(LogFile)) return true;
|
||||
if (!_buffer.ContainsKey(LogFile))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var tries = 0;
|
||||
var completed = false;
|
||||
@@ -193,7 +200,10 @@ public class FileLoggerProvider : BatchingLoggerProvider
|
||||
{
|
||||
var hasPrefix = !string.IsNullOrWhiteSpace(_fileNamePrefix);
|
||||
if (hasPrefix)
|
||||
{
|
||||
return Path.Combine(_path, $"{_fileNamePrefix}_{group.Year:0000}{group.Month:00}{group.Day:00}.log");
|
||||
}
|
||||
|
||||
return Path.Combine(_path, $"{group.Year:0000}{group.Month:00}{group.Day:00}.log");
|
||||
}
|
||||
|
||||
@@ -260,7 +270,7 @@ public class FileLoggerProvider : BatchingLoggerProvider
|
||||
private async Task WriteEndMessageAsync(string logFilePath)
|
||||
{
|
||||
var stopMessage = LogHelper.GetStopMessage();
|
||||
stopMessage = LogHelper.FormatMessageWithHeader(LoggerSettings, ELogType.INFO, stopMessage, CurrentDateTme);
|
||||
stopMessage = LogHelper.FormatMessageWithHeader(LoggerSettings, ELogType.INFO, stopMessage, CurrentDateTme, Category);
|
||||
|
||||
using (var file = new StreamWriter(logFilePath, true))
|
||||
{
|
||||
@@ -292,13 +302,20 @@ public class FileLoggerProvider : BatchingLoggerProvider
|
||||
IEnumerable<FileInfo> files = null;
|
||||
|
||||
if (hasPrefix)
|
||||
{
|
||||
files = new DirectoryInfo(_path).GetFiles(_fileNamePrefix + "*");
|
||||
}
|
||||
else
|
||||
{
|
||||
files = new DirectoryInfo(_path).GetFiles("*");
|
||||
}
|
||||
|
||||
files = files.OrderByDescending(file => file.Name).Skip(_maxRetainedFiles);
|
||||
|
||||
foreach (var item in files) item.Delete();
|
||||
foreach (var item in files)
|
||||
{
|
||||
item.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,67 +4,70 @@ using EonaCat.Logger.Extensions;
|
||||
using EonaCat.Logger.Managers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EonaCat.Logger.EonaCatCoreLogger.Internal;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public class BatchingLogger : ILogger
|
||||
namespace EonaCat.Logger.EonaCatCoreLogger.Internal
|
||||
{
|
||||
private readonly string _category;
|
||||
private readonly BatchingLoggerProvider _provider;
|
||||
private LoggerSettings _loggerSettings;
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
public BatchingLogger(BatchingLoggerProvider loggerProvider, string categoryName, LoggerSettings loggerSettings)
|
||||
public class BatchingLogger : ILogger
|
||||
{
|
||||
_loggerSettings = loggerSettings;
|
||||
_provider = loggerProvider;
|
||||
_category = categoryName;
|
||||
}
|
||||
private readonly string _category;
|
||||
private readonly BatchingLoggerProvider _provider;
|
||||
private readonly LoggerSettings _loggerSettings;
|
||||
|
||||
private DateTimeOffset CurrentDateTimeOffset =>
|
||||
_loggerSettings.UseLocalTime ? DateTimeOffset.Now : DateTimeOffset.UtcNow;
|
||||
|
||||
private DateTime CurrentDateTme => _loggerSettings.UseLocalTime ? DateTime.Now : DateTime.UtcNow;
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel)
|
||||
{
|
||||
return logLevel != LogLevel.None;
|
||||
}
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
|
||||
Func<TState, Exception, string> formatter)
|
||||
{
|
||||
Log(CurrentDateTimeOffset, logLevel, eventId, state, exception, formatter);
|
||||
}
|
||||
|
||||
public void Log<TState>(DateTimeOffset timestamp, LogLevel logLevel, EventId eventId, TState state,
|
||||
Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
if (!IsEnabled(logLevel)) return;
|
||||
|
||||
if (_loggerSettings == null) _loggerSettings = new LoggerSettings();
|
||||
|
||||
var message = LogHelper.FormatMessageWithHeader(_loggerSettings, logLevel.FromLogLevel(),
|
||||
formatter(state, exception), timestamp.DateTime) + Environment.NewLine;
|
||||
if (exception != null) message = exception.FormatExceptionToMessage() + Environment.NewLine;
|
||||
|
||||
_provider.AddMessage(timestamp, message);
|
||||
|
||||
var currentMessage = new EonaCatLogMessage
|
||||
public BatchingLogger(BatchingLoggerProvider loggerProvider, string categoryName, LoggerSettings loggerSettings)
|
||||
{
|
||||
DateTime = timestamp.DateTime,
|
||||
Message = message,
|
||||
LogType = logLevel.FromLogLevel()
|
||||
};
|
||||
_provider = loggerProvider ?? throw new ArgumentNullException(nameof(loggerProvider));
|
||||
_category = categoryName ?? throw new ArgumentNullException(nameof(categoryName));
|
||||
_loggerSettings = loggerSettings ?? throw new ArgumentNullException(nameof(loggerSettings));
|
||||
}
|
||||
|
||||
currentMessage.Origin = string.IsNullOrWhiteSpace(_loggerSettings.LogOrigin)
|
||||
? "BatchingLogger"
|
||||
: _loggerSettings.LogOrigin;
|
||||
_loggerSettings?.OnLogEvent(currentMessage);
|
||||
private DateTimeOffset CurrentDateTimeOffset => CurrentDateTime;
|
||||
|
||||
private DateTime CurrentDateTime => _loggerSettings.UseLocalTime ? DateTime.Now : DateTime.UtcNow;
|
||||
|
||||
public IDisposable BeginScope<TState>(TState state) => null;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
|
||||
Func<TState, Exception, string> formatter)
|
||||
{
|
||||
if (!IsEnabled(logLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var timestamp = CurrentDateTimeOffset;
|
||||
Log(timestamp, logLevel, eventId, state, exception, formatter, _category);
|
||||
}
|
||||
|
||||
public void Log<TState>(DateTimeOffset timestamp, LogLevel logLevel, EventId eventId, TState state,
|
||||
Exception exception, Func<TState, Exception, string> formatter, string category)
|
||||
{
|
||||
if (!IsEnabled(logLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string message = exception != null
|
||||
? exception.FormatExceptionToMessage() + Environment.NewLine
|
||||
: formatter(state, exception);
|
||||
|
||||
message = LogHelper.FormatMessageWithHeader(_loggerSettings, logLevel.FromLogLevel(), message, timestamp.DateTime, category)
|
||||
+ Environment.NewLine;
|
||||
|
||||
_provider.AddMessage(timestamp, message);
|
||||
|
||||
var currentMessage = new EonaCatLogMessage
|
||||
{
|
||||
DateTime = timestamp.DateTime,
|
||||
Message = message,
|
||||
LogType = logLevel.FromLogLevel(),
|
||||
Origin = string.IsNullOrWhiteSpace(_loggerSettings.LogOrigin) ? "BatchingLogger" : _loggerSettings.LogOrigin
|
||||
};
|
||||
|
||||
_loggerSettings.OnLogEvent(currentMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,10 @@ public class BatchingLoggerOptions
|
||||
set
|
||||
{
|
||||
if (value <= TimeSpan.Zero)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(FlushPeriod)} must be positive.");
|
||||
}
|
||||
|
||||
_flushPeriod = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,16 +23,23 @@ public abstract class BatchingLoggerProvider : ILoggerProvider, IDisposable
|
||||
private Task _outputTask;
|
||||
private object _writeLock = new object();
|
||||
private bool _isDisposing;
|
||||
protected string Category;
|
||||
|
||||
protected BatchingLoggerProvider(IOptions<BatchingLoggerOptions> options)
|
||||
{
|
||||
var loggerOptions = options.Value;
|
||||
|
||||
if (loggerOptions.FlushPeriod <= TimeSpan.Zero)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(loggerOptions.FlushPeriod),
|
||||
$"{nameof(loggerOptions.FlushPeriod)} must be longer than zero.");
|
||||
}
|
||||
|
||||
if (options.Value is FileLoggerOptions fileLoggerOptions)
|
||||
{
|
||||
UseLocalTime = fileLoggerOptions.UseLocalTime;
|
||||
}
|
||||
|
||||
if (options.Value is FileLoggerOptions fileLoggerOptions) UseLocalTime = fileLoggerOptions.UseLocalTime;
|
||||
_batchSize = loggerOptions.BatchSize;
|
||||
|
||||
StartAsync().ConfigureAwait(false);
|
||||
@@ -47,7 +54,10 @@ public abstract class BatchingLoggerProvider : ILoggerProvider, IDisposable
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_loggerSettings != null) return _loggerSettings;
|
||||
if (_loggerSettings != null)
|
||||
{
|
||||
return _loggerSettings;
|
||||
}
|
||||
|
||||
_loggerSettings = new LoggerSettings();
|
||||
_loggerSettings.UseLocalTime = UseLocalTime;
|
||||
@@ -73,6 +83,7 @@ public abstract class BatchingLoggerProvider : ILoggerProvider, IDisposable
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
{
|
||||
Category = categoryName;
|
||||
return new BatchingLogger(this, categoryName, LoggerSettings);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user