|
| 1 | +using System.IO.Abstractions; |
| 2 | +using GitVersion.Extensions; |
| 3 | +using GitVersion.Helpers; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Serilog; |
| 7 | +using Serilog.Events; |
| 8 | +using Serilog.Sinks.SystemConsole.Themes; |
| 9 | + |
| 10 | +namespace GitVersion.Logging; |
| 11 | + |
| 12 | +public class LoggingModule |
| 13 | +{ |
| 14 | + public static IServiceCollection AddLogging( |
| 15 | + IServiceCollection services, |
| 16 | + Verbosity verbosity = Verbosity.Normal, |
| 17 | + bool addConsole = false, |
| 18 | + string? logFilePath = null, |
| 19 | + IFileSystem? fileSystem = null) |
| 20 | + { |
| 21 | + var loggerConfiguration = new LoggerConfiguration() |
| 22 | + .MinimumLevel.Verbose() |
| 23 | + .Enrich.FromLogContext(); |
| 24 | + |
| 25 | + if (addConsole) |
| 26 | + { |
| 27 | + loggerConfiguration = AddConsoleLogger(loggerConfiguration, verbosity); |
| 28 | + } |
| 29 | + |
| 30 | + if (!string.IsNullOrWhiteSpace(logFilePath) && fileSystem != null) |
| 31 | + { |
| 32 | + loggerConfiguration = AddFileLogger(loggerConfiguration, fileSystem, logFilePath, verbosity); |
| 33 | + } |
| 34 | + |
| 35 | + Log.Logger = loggerConfiguration.CreateLogger(); |
| 36 | + |
| 37 | + services.AddLogging(builder => |
| 38 | + { |
| 39 | + builder.ClearProviders(); |
| 40 | + builder.AddSerilog(Log.Logger, dispose: true); |
| 41 | + builder.SetMinimumLevel(MapVerbosityToMicrosoftLogLevel(verbosity)); |
| 42 | + }); |
| 43 | + |
| 44 | + return services; |
| 45 | + } |
| 46 | + |
| 47 | + private static LoggerConfiguration AddConsoleLogger(LoggerConfiguration configuration, Verbosity verbosity) |
| 48 | + { |
| 49 | + var logLevel = MapVerbosityToSerilogLevel(verbosity); |
| 50 | + |
| 51 | + return configuration.WriteTo.Console( |
| 52 | + restrictedToMinimumLevel: logLevel, |
| 53 | + outputTemplate: "{Level:u} [{Timestamp:yy-MM-dd H:mm:ss:ff}] {Message:lj}{NewLine}{Exception}", |
| 54 | + theme: AnsiConsoleTheme.Code, |
| 55 | + applyThemeToRedirectedOutput: true |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + private static LoggerConfiguration AddFileLogger( |
| 60 | + LoggerConfiguration configuration, |
| 61 | + IFileSystem fileSystem, |
| 62 | + string filePath, |
| 63 | + Verbosity verbosity) |
| 64 | + { |
| 65 | + fileSystem.NotNull(); |
| 66 | + |
| 67 | + var logFile = fileSystem.FileInfo.New(FileSystemHelper.Path.GetFullPath(filePath)); |
| 68 | + logFile.Directory?.Create(); |
| 69 | + |
| 70 | + var logLevel = MapVerbosityToSerilogLevel(verbosity); |
| 71 | + |
| 72 | + return configuration.WriteTo.File( |
| 73 | + path: filePath, |
| 74 | + restrictedToMinimumLevel: logLevel, |
| 75 | + outputTemplate: "{Level:u} [{Timestamp:yyyy-MM-dd HH:mm:ss}] {Message:lj}{NewLine}{Exception}", |
| 76 | + shared: true, |
| 77 | + flushToDiskInterval: TimeSpan.FromSeconds(1) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + private static LogEventLevel MapVerbosityToSerilogLevel(Verbosity verbosity) => verbosity switch |
| 82 | + { |
| 83 | + Verbosity.Quiet => LogEventLevel.Error, |
| 84 | + Verbosity.Minimal => LogEventLevel.Warning, |
| 85 | + Verbosity.Normal => LogEventLevel.Information, |
| 86 | + Verbosity.Verbose => LogEventLevel.Verbose, |
| 87 | + Verbosity.Diagnostic => LogEventLevel.Debug, |
| 88 | + _ => LogEventLevel.Information |
| 89 | + }; |
| 90 | + |
| 91 | + private static Microsoft.Extensions.Logging.LogLevel MapVerbosityToMicrosoftLogLevel(Verbosity verbosity) => verbosity switch |
| 92 | + { |
| 93 | + Verbosity.Quiet => Microsoft.Extensions.Logging.LogLevel.Error, |
| 94 | + Verbosity.Minimal => Microsoft.Extensions.Logging.LogLevel.Warning, |
| 95 | + Verbosity.Normal => Microsoft.Extensions.Logging.LogLevel.Information, |
| 96 | + Verbosity.Verbose => Microsoft.Extensions.Logging.LogLevel.Debug, |
| 97 | + Verbosity.Diagnostic => Microsoft.Extensions.Logging.LogLevel.Trace, |
| 98 | + _ => Microsoft.Extensions.Logging.LogLevel.Information |
| 99 | + }; |
| 100 | +} |
0 commit comments