|
|
@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
using static WF_WebAdmin.Model.LoggerSaveStub.CustomLoggerConfiguration;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace WF_WebAdmin.Model
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public sealed class LoggerSaveStub : ILogger
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
internal class CustomLoggerConfiguration
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public int EventId { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Dictionary<LogLevel, LogFormat> LogLevels { get; set; } =
|
|
|
|
|
|
|
|
new()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
[LogLevel.Information] = LogFormat.Short,
|
|
|
|
|
|
|
|
[LogLevel.Warning] = LogFormat.Short,
|
|
|
|
|
|
|
|
[LogLevel.Error] = LogFormat.Long
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public enum LogFormat
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Short,
|
|
|
|
|
|
|
|
Long
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly string name;
|
|
|
|
|
|
|
|
private readonly Func<CustomLoggerConfiguration> getCurrentConfig;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IDisposable BeginScope<TState>(TState state) => default!;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsEnabled(LogLevel logLevel) =>
|
|
|
|
|
|
|
|
getCurrentConfig().LogLevels.ContainsKey(logLevel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Log<TState>(
|
|
|
|
|
|
|
|
LogLevel logLevel,
|
|
|
|
|
|
|
|
EventId eventId,
|
|
|
|
|
|
|
|
TState state,
|
|
|
|
|
|
|
|
Exception? exception,
|
|
|
|
|
|
|
|
Func<TState, Exception?, string> formatter)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!IsEnabled(logLevel))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CustomLoggerConfiguration config = getCurrentConfig();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (config.EventId == 0 || config.EventId == eventId.Id)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (config.LogLevels[logLevel])
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case LogFormat.Short:
|
|
|
|
|
|
|
|
Console.WriteLine($"{name}: {formatter(state, exception)}");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LogFormat.Long:
|
|
|
|
|
|
|
|
Console.WriteLine($"[{eventId.Id,2}: {logLevel,-12}] {name} - {formatter(state, exception)}");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
// No-op
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|