diff options
author | Wouter Tinus <win.acme.simple@gmail.com> | 2020-01-09 22:04:48 +0100 |
---|---|---|
committer | Wouter Tinus <win.acme.simple@gmail.com> | 2020-01-09 22:04:48 +0100 |
commit | 9eb22af2cf9e94125c073295a237dad782bc9923 (patch) | |
tree | b72a9e51d540be9442c5ca1b4faa55d9d9003d96 /src/main.lib/Services/LogService.cs | |
parent | 4c302cc321e2dca32bd676791f3a5f322aba876b (diff) | |
download | letsencrypt-win-simple-9eb22af2cf9e94125c073295a237dad782bc9923.zip letsencrypt-win-simple-9eb22af2cf9e94125c073295a237dad782bc9923.tar.gz letsencrypt-win-simple-9eb22af2cf9e94125c073295a237dad782bc9923.tar.bz2 |
improve support for custom logging and add example to docs, also include ProcessId as possible context to the disk logger
Diffstat (limited to 'src/main.lib/Services/LogService.cs')
-rw-r--r-- | src/main.lib/Services/LogService.cs | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/src/main.lib/Services/LogService.cs b/src/main.lib/Services/LogService.cs index 479a20f..7872abb 100644 --- a/src/main.lib/Services/LogService.cs +++ b/src/main.lib/Services/LogService.cs @@ -4,6 +4,7 @@ using Serilog.Core; using Serilog.Events; using Serilog.Sinks.SystemConsole.Themes; using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -16,15 +17,13 @@ namespace PKISharp.WACS.Services private Logger? _diskLogger; private readonly LoggingLevelSwitch _levelSwitch; public bool Dirty { get; set; } - private IConfigurationRoot ConfigurationRoot { get; } + private string _configurationPath { get; } public LogService() { // Custom configuration support var installDir = new FileInfo(Process.GetCurrentProcess().MainModule.FileName).DirectoryName; - ConfigurationRoot = new ConfigurationBuilder() - .AddJsonFile(Path.Combine(installDir, "serilog.json"), true, true) - .Build(); + _configurationPath = Path.Combine(installDir, "serilog.json"); #if DEBUG var initialLevel = LogEventLevel.Debug; #else @@ -37,8 +36,7 @@ namespace PKISharp.WACS.Services .MinimumLevel.ControlledBy(_levelSwitch) .Enrich.FromLogContext() .Filter.ByIncludingOnly(x => { Dirty = true; return true; }) - .WriteTo.Console(outputTemplate: " [{Level:u4}] {Message:l}{NewLine}{Exception}", theme: SystemConsoleTheme.Literate) - .ReadFrom.Configuration(ConfigurationRoot, "screen") + .WriteTo.Console(outputTemplate: " [{Level:u4}] {Message:l}{NewLine}{Exception}", theme: AnsiConsoleTheme.Code) .CreateLogger(); } catch (Exception ex) @@ -52,11 +50,15 @@ namespace PKISharp.WACS.Services try { + var _eventConfig = new ConfigurationBuilder() + .AddJsonFile(_configurationPath, true, true) + .Build(); + _eventLogger = new LoggerConfiguration() .MinimumLevel.ControlledBy(_levelSwitch) .Enrich.FromLogContext() .WriteTo.EventLog("win-acme", manageEventSource: true) - .ReadFrom.Configuration(ConfigurationRoot, "event") + .ReadFrom.Configuration(_eventConfig, "event") .CreateLogger(); } catch (Exception ex) @@ -70,11 +72,35 @@ namespace PKISharp.WACS.Services { try { + var defaultPath = path.TrimEnd('\\', '/') + "\\log-.txt"; + var defaultRollingInterval = RollingInterval.Day; + var fileConfig = new ConfigurationBuilder() + .AddJsonFile(_configurationPath, true, true) + .Build(); + + foreach (var writeTo in fileConfig.GetSection("disk:WriteTo").GetChildren()) + { + if (writeTo.GetValue<string>("Name") == "File") + { + var pathSection = writeTo.GetSection("Args:path"); + if (string.IsNullOrEmpty(pathSection.Value)) + { + pathSection.Value = defaultPath; + } + var rollingInterval = writeTo.GetSection("Args:rollingInterval"); + if (string.IsNullOrEmpty(rollingInterval.Value)) + { + rollingInterval.Value = ((int)defaultRollingInterval).ToString(); + } + } + } + _diskLogger = new LoggerConfiguration() .MinimumLevel.ControlledBy(_levelSwitch) .Enrich.FromLogContext() - .WriteTo.File(path.TrimEnd('\\', '/') + "\\log-.txt", rollingInterval: RollingInterval.Day) - .ReadFrom.Configuration(ConfigurationRoot, "disk") + .Enrich.WithProperty("ProcessId", Process.GetCurrentProcess().Id) + .WriteTo.File(defaultPath, rollingInterval: defaultRollingInterval) + .ReadFrom.Configuration(fileConfig, "disk") .CreateLogger(); } catch (Exception ex) @@ -103,7 +129,7 @@ namespace PKISharp.WACS.Services public void Information(LogType logType, string message, params object?[] items) => _Information(logType, message, items); - private void Verbose(LogType type, string message, params object?[] items) => Write(type, LogEventLevel.Verbose, message, items); + public void Verbose(LogType type, string message, params object?[] items) => Write(type, LogEventLevel.Verbose, message, items); private void Debug(LogType type, string message, params object?[] items) => Write(type, LogEventLevel.Debug, message, items); |