| | | 1 | | #region Usings |
| | | 2 | | using System.Globalization; |
| | | 3 | | using Serilog; |
| | | 4 | | using ZhEaIsNsAaBn.Utilities.Logging; |
| | | 5 | | #endregion |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 9 | | |
| | | 10 | | /// <summary>Registers the ZhEaIsNsAaBn Serilog defaults. Call it once, from the app's startup.</summary> |
| | | 11 | | public static class SerilogRegistration |
| | | 12 | | { |
| | | 13 | | private const string LogFileName = "log.log"; |
| | | 14 | | private const long FileSizeLimitBytes = 100 * 1024 * 1024; |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Configures Serilog as the app's logger and starts the SerilogTracing listener (STYLE_GUIDE §7): |
| | | 19 | | /// enrichers (demystified stack traces, version, client IP, correlation id, machine, memory, process), an async dai |
| | | 20 | | /// rolling file of up to 100 MB, and async Console and Debug sinks outside production. Values are formatted with th |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="services">The service collection.</param> |
| | | 23 | | /// <param name="logFilePath">Folder for the log files. Files are named <c>log<date>.log</c>.</param> |
| | | 24 | | /// <param name="isProduction"><see langword="true"/> for <c>Information</c> and above, file only; <see langword="fa |
| | | 25 | | /// <returns>The same <paramref name="services"/>, for chaining.</returns> |
| | | 26 | | /// <exception cref="ArgumentNullException"><paramref name="services"/> or <paramref name="logFilePath"/> is <see la |
| | | 27 | | /// <exception cref="ArgumentException"><paramref name="logFilePath"/> is empty or white space.</exception> |
| | | 28 | | /// <example> |
| | | 29 | | /// <code> |
| | | 30 | | /// builder.Services.AddSerilogDefaults( "Logs", isProduction: builder.Environment.IsProduction() ); |
| | | 31 | | /// </code> |
| | | 32 | | /// </example> |
| | | 33 | | public static IServiceCollection AddSerilogDefaults( this IServiceCollection services, string logFilePath, bool isPr |
| | | 34 | | { |
| | 24 | 35 | | ArgumentNullException.ThrowIfNull( services ); |
| | 24 | 36 | | ArgumentException.ThrowIfNullOrWhiteSpace( logFilePath ); |
| | | 37 | | |
| | | 38 | | |
| | 16 | 39 | | services.AddHostedService<SerilogTracingListener>(); |
| | | 40 | | |
| | | 41 | | |
| | 16 | 42 | | return services.AddSerilog( configuration => |
| | 16 | 43 | | { |
| | 16 | 44 | | if ( isProduction ) { configuration.MinimumLevel.Information(); } |
| | 16 | 45 | | else { configuration.MinimumLevel.Verbose(); } |
| | 16 | 46 | | |
| | 16 | 47 | | |
| | 16 | 48 | | configuration.Enrich.WithDemystifiedStackTraces() |
| | 16 | 49 | | .Enrich.WithAssemblyInformationalVersion() |
| | 16 | 50 | | .Enrich.WithClientIp() |
| | 16 | 51 | | .Enrich.WithCorrelationId() |
| | 16 | 52 | | .Enrich.WithMachineName() |
| | 16 | 53 | | .Enrich.WithMemoryUsage() |
| | 16 | 54 | | .Enrich.WithProcessId() |
| | 16 | 55 | | .Enrich.WithProcessName(); |
| | 16 | 56 | | |
| | 16 | 57 | | |
| | 16 | 58 | | if ( !isProduction ) |
| | 16 | 59 | | { |
| | 16 | 60 | | configuration.WriteTo.Async( sink => sink.Console( formatProvider: CultureInfo.InvariantCulture ), block |
| | 16 | 61 | | .WriteTo.Async( sink => sink.Debug( formatProvider: CultureInfo.InvariantCulture ), blockWh |
| | 16 | 62 | | } |
| | 16 | 63 | | |
| | 16 | 64 | | |
| | 16 | 65 | | configuration.WriteTo.Async( sink => sink.File( Path.Combine( logFilePath, LogFileName ), |
| | 16 | 66 | | rollingInterval: RollingInterval.Day, |
| | 16 | 67 | | fileSizeLimitBytes: FileSizeLimitBytes, |
| | 16 | 68 | | shared: true, |
| | 16 | 69 | | formatProvider: CultureInfo.InvariantCulture ), |
| | 16 | 70 | | blockWhenFull: true ); |
| | 16 | 71 | | } ); |
| | | 72 | | } |
| | | 73 | | } |