55 lines
2.1 KiB
C#
55 lines
2.1 KiB
C#
using DotBased.Logging;
|
|
using DotBased.Logging.MEL;
|
|
using DotBased.Logging.Serilog;
|
|
using Serilog;
|
|
|
|
namespace Manager.App;
|
|
|
|
public static class DependencyInjection
|
|
{
|
|
public static WebApplicationBuilder InjectDependencies(this WebApplicationBuilder builder)
|
|
{
|
|
return builder;
|
|
}
|
|
|
|
public static void SetupLogging(this WebApplicationBuilder builder)
|
|
{
|
|
var isDevelopment = builder.Environment.IsDevelopment();
|
|
var logSeverity = isDevelopment ? LogSeverity.Debug : LogSeverity.Info;
|
|
var severityFilters = new Dictionary<string, LogSeverity>();
|
|
|
|
var dotBasedLogSection = builder.Configuration.GetSection("DotBased:Logging");
|
|
if (dotBasedLogSection.Exists())
|
|
{
|
|
logSeverity = dotBasedLogSection.GetValue<LogSeverity>("Severity");
|
|
severityFilters = dotBasedLogSection.GetSection("SeverityFilters").GetChildren()
|
|
.ToDictionary(
|
|
x => x.Key,
|
|
x => x.Get<LogSeverity>()
|
|
);
|
|
}
|
|
|
|
LogService.Initialize(options =>
|
|
{
|
|
options.Severity = logSeverity;
|
|
foreach (var filter in severityFilters)
|
|
{
|
|
options.AddSeverityFilter(filter.Key, filter.Value);
|
|
}
|
|
});
|
|
Log.Logger = new LoggerConfiguration().UseBasedExtension()
|
|
.MinimumLevel.Verbose()
|
|
.Enrich.WithProperty("Application", "ImportUI")
|
|
.WriteTo.Console(outputTemplate: BasedSerilog.OutputTemplate)
|
|
.WriteTo.File(path: Path.Combine("Logs", $"{(isDevelopment ? "Debug" : "Release")}", "log_.log"), rollingInterval: RollingInterval.Day, outputTemplate: BasedSerilog.OutputTemplate)
|
|
.Destructure.ToMaximumDepth(4)
|
|
.Destructure.ToMaximumStringLength(100)
|
|
.Destructure.ToMaximumCollectionCount(10).CreateLogger();
|
|
|
|
LogService.AddLogAdapter(new BasedSerilogAdapter(Log.Logger));
|
|
|
|
builder.Logging.ClearProviders();
|
|
builder.Logging.SetMinimumLevel(isDevelopment ? LogLevel.Trace : LogLevel.Information);
|
|
builder.Logging.AddDotBasedLoggerProvider(LogService.Options);
|
|
}
|
|
} |