Hermod
A cross-platform, modular and fully GDPR-compliant email archival solution!
Loading...
Searching...
No Matches
Logger.cs
Go to the documentation of this file.
1using System;
2
3namespace Hermod.Core {
4
5 using Serilog;
6 using Serilog.Events;
7 using Serilog.Formatting.Json;
8
12 public class Logger {
13
17 public bool EnableConsoleOutput { get; set; } = false;
18
19 #if DEBUG
20 public bool EnableDebugOutput { get; set; } = true;
21 #else
22 bool EnableDebugOutput { get; set; } = false;
23 #endif
24
25 public bool EnableFileOutput { get; set; } = true;
26
27 #if DEBUG
28 public LogEventLevel ConsoleLogLevel { get; set; } = LogEventLevel.Warning;
29 #else
30 public LogEventLevel ConsoleLogLevel { get; set; } = LogEventLevel.Debug;
31 #endif
32
33 public LogEventLevel FileLogLevel { get; set; } = LogEventLevel.Information;
34
35 public FileInfo? LogFilePath { get; set; } = null;
36
37 public long MaxLogFileSize { get; set; } = (long)Math.Pow(1024, 3) * 5; // default is 5MiB
38
39 public RollingInterval FileRollingInterval { get; set; } = RollingInterval.Day;
40
41 public bool RollOnFileSizeLimit { get; set; } = true;
42
43 private ILogger? m_logger;
44
45 public Logger() { }
46
47 public Logger(bool enableConsole, bool enableDebug, bool enableFile) {
48 EnableConsoleOutput = enableConsole;
49 EnableDebugOutput = enableDebug;
50 EnableFileOutput = enableFile;
51 }
52
56 protected ILogger CreateLogger() {
57 var logCfg = new LoggerConfiguration();
59 logCfg.WriteTo.Console(ConsoleLogLevel, applyThemeToRedirectedOutput: true);
60 }
62 logCfg.WriteTo.Debug(LogEventLevel.Debug);
63 }
64 if (EnableFileOutput && LogFilePath != null) {
65 logCfg.WriteTo.File(
66 new JsonFormatter(),
67 LogFilePath.FullName,
70 rollingInterval: FileRollingInterval,
71 rollOnFileSizeLimit: RollOnFileSizeLimit,
72 shared: true
73
74 );
75 }
76
77 m_logger = logCfg.CreateLogger();
78 return m_logger;
79 }
80
85 public ILogger GetLogger() => m_logger ??= CreateLogger();
86
87 }
88}
89
Custom logger class for Hermod; internally uses Serilog.
Definition: Logger.cs:12
LogEventLevel ConsoleLogLevel
Definition: Logger.cs:30
bool EnableFileOutput
Definition: Logger.cs:25
bool EnableDebugOutput
Definition: Logger.cs:22
ILogger? m_logger
Definition: Logger.cs:43
bool EnableConsoleOutput
Gets or sets a value indicating whether or not to enable console logging.
Definition: Logger.cs:17
bool RollOnFileSizeLimit
Definition: Logger.cs:41
FileInfo? LogFilePath
Definition: Logger.cs:35
RollingInterval FileRollingInterval
Definition: Logger.cs:39
Logger(bool enableConsole, bool enableDebug, bool enableFile)
Definition: Logger.cs:47
ILogger CreateLogger()
Creates a new instance of the Serilog logger.
Definition: Logger.cs:56
LogEventLevel FileLogLevel
Definition: Logger.cs:33
ILogger GetLogger()
Gets the instance of the Serilog logger.
long MaxLogFileSize
Definition: Logger.cs:37