-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SerilogConfigurator.cs
53 lines (45 loc) · 1.68 KB
/
SerilogConfigurator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.IO;
using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
namespace Service
{
internal static class SerilogConfigurator
{
private static readonly LoggingLevelSwitch LevelSwitch = new LoggingLevelSwitch();
public static LoggerConfiguration Configure()
{
var minimumLevel = GetMinimumLogLevel();
SetMinimumLogLevel(minimumLevel);
return new LoggerConfiguration()
.MinimumLevel.ControlledBy(LevelSwitch)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console(theme: AnsiConsoleTheme.Code);
}
private static string GetMinimumLogLevel()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
var logValue = configuration.GetSection("Logging")["MinimumLevel"];
return string.IsNullOrEmpty(logValue) ? "info" : logValue;
}
public static void SetMinimumLogLevel(string level)
{
LevelSwitch.MinimumLevel = level.ToLower() switch
{
"info" => LogEventLevel.Information,
"debug" => LogEventLevel.Debug,
"error" => LogEventLevel.Error,
"warning" => LogEventLevel.Warning,
"trace" => LogEventLevel.Verbose,
_ => LogEventLevel.Information
};
}
}
}