-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingUtils.cs
More file actions
69 lines (58 loc) · 2.03 KB
/
LoggingUtils.cs
File metadata and controls
69 lines (58 loc) · 2.03 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
namespace Couchbase.Analytics.Performer.Internal.Logging;
public static class LoggingUtils
{
private const string LogLevelEnvVarName = "LOG_LEVEL";
public static ILoggerFactory ConfigureLogging(out LogEventLevel minimumLevel)
{
var envLogLevel = Environment.GetEnvironmentVariable(LogLevelEnvVarName);
minimumLevel = ParseLogLevelOrDefault(envLogLevel, LogEventLevel.Information);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(minimumLevel)
.Enrich.FromLogContext()
.WriteTo.Console()
// .WriteTo.File(
// path: "Logs/analytics-performer.log",
// rollingInterval: RollingInterval.Day,
// retainedFileCountLimit: 7,
// shared: true)
.CreateLogger();
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.ClearProviders();
builder.AddSerilog();
});
return loggerFactory;
}
public static void ShutdownLogging()
{
Log.CloseAndFlush();
}
public static LogEventLevel ParseLogLevelOrDefault(string? value, LogEventLevel defaultLevel)
{
if (string.IsNullOrWhiteSpace(value))
{
return defaultLevel;
}
if (Enum.TryParse<LogEventLevel>(value, true, out var serilogLevel))
{
return serilogLevel;
}
if (Enum.TryParse<LogLevel>(value, true, out var msLevel))
{
return msLevel switch
{
LogLevel.Trace => LogEventLevel.Verbose,
LogLevel.Debug => LogEventLevel.Debug,
LogLevel.Information => LogEventLevel.Information,
LogLevel.Warning => LogEventLevel.Warning,
LogLevel.Error => LogEventLevel.Error,
LogLevel.Critical => LogEventLevel.Fatal,
_ => defaultLevel
};
}
return defaultLevel;
}
}