72 lines
2.5 KiB
C#
72 lines
2.5 KiB
C#
using EonaCat.logger.Managers;
|
|
using EonaCat.Logger.Managers;
|
|
using EonaCatLogger.EonaCatCoreLogger.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Runtime;
|
|
|
|
namespace EonaCat.Logger.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
|
|
{
|
|
private LoggerSettings _loggerSettings;
|
|
private readonly BatchingLoggerProvider _provider;
|
|
private readonly string _category;
|
|
|
|
public BatchingLogger(BatchingLoggerProvider loggerProvider, string categoryName, LoggerSettings loggerSettings = null)
|
|
{
|
|
_loggerSettings = loggerSettings;
|
|
_provider = loggerProvider;
|
|
_category = categoryName;
|
|
}
|
|
|
|
public IDisposable BeginScope<TState>(TState state)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public bool IsEnabled(LogLevel logLevel)
|
|
{
|
|
return logLevel != LogLevel.None;
|
|
}
|
|
|
|
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), DateTime.Now) + Environment.NewLine;
|
|
if (exception != null)
|
|
{
|
|
message = exception.FormatExceptionToMessage() + Environment.NewLine;
|
|
}
|
|
|
|
_provider.AddMessage(timestamp, message);
|
|
|
|
var EonaCatMessage = new EonaCatLogMessage
|
|
{
|
|
DateTime = timestamp.DateTime,
|
|
Message = message,
|
|
LogType = logLevel.FromLogLevel()
|
|
};
|
|
|
|
if (_loggerSettings != null)
|
|
{
|
|
EonaCatMessage.Origin = string.IsNullOrWhiteSpace(_loggerSettings.LogOrigin) ? "BatchingLogger" : _loggerSettings.LogOrigin;
|
|
_loggerSettings?.OnLogEvent(EonaCatMessage);
|
|
}
|
|
}
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
|
{
|
|
Log(DateTimeOffset.Now, logLevel, eventId, state, exception, formatter);
|
|
}
|
|
}
|
|
} |