This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathLogManager.cs
More file actions
59 lines (50 loc) · 2.15 KB
/
Copy pathLogManager.cs
File metadata and controls
59 lines (50 loc) · 2.15 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
using System;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using GitHub.Info;
using Serilog;
using Serilog.Core;
using Serilog.Events;
namespace GitHub.Logging
{
public static class LogManager
{
#if DEBUG
private static LogEventLevel DefaultLoggingLevel = LogEventLevel.Debug;
#else
private static LogEventLevel DefaultLoggingLevel = LogEventLevel.Information;
#endif
private static LoggingLevelSwitch LoggingLevelSwitch = new LoggingLevelSwitch(DefaultLoggingLevel);
static Logger CreateLogger()
{
var logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
ApplicationInfo.ApplicationName,
"extension.log");
const string outputTemplate =
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{ProcessId:00000}] {Level:u4} [{ThreadId:00}] {ShortSourceContext,-25} {Message:lj}{NewLine}{Exception}";
return new LoggerConfiguration()
.Enrich.WithProcessId()
.Enrich.WithThreadId()
.MinimumLevel.ControlledBy(LoggingLevelSwitch)
.WriteTo.File(logPath,
fileSizeLimitBytes: null,
outputTemplate: outputTemplate,
shared: true)
.CreateLogger();
}
public static void EnableTraceLogging(bool enable)
{
var logEventLevel = enable ? LogEventLevel.Verbose : DefaultLoggingLevel;
if(LoggingLevelSwitch.MinimumLevel != logEventLevel)
{
ForContext(typeof(LogManager)).Information("Set Logging Level: {LogEventLevel}", logEventLevel);
LoggingLevelSwitch.MinimumLevel = logEventLevel;
}
}
static Lazy<Logger> Logger { get; } = new Lazy<Logger>(CreateLogger);
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
public static ILogger ForContext<T>() => ForContext(typeof(T));
public static ILogger ForContext(Type type) => Logger.Value.ForContext(type).ForContext("ShortSourceContext", type.Name);
}
}