< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: Microsoft.Extensions.DependencyInjection.SerilogRegistration
Assembly: ZhEaIsNsAaBn.Utilities.Logging
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.Logging/SerilogRegistration.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 73
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddSerilogDefaults(...)100%11100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/UtilitiesPackages/ZhEaIsNsAaBn.Utilities.Logging/SerilogRegistration.cs

#LineLine coverage
 1#region Usings
 2using System.Globalization;
 3using Serilog;
 4using ZhEaIsNsAaBn.Utilities.Logging;
 5#endregion
 6
 7// ReSharper disable once CheckNamespace
 8namespace Microsoft.Extensions.DependencyInjection;
 9
 10/// <summary>Registers the ZhEaIsNsAaBn Serilog defaults. Call it once, from the app's startup.</summary>
 11public 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&lt;date&gt;.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    {
 2435        ArgumentNullException.ThrowIfNull( services );
 2436        ArgumentException.ThrowIfNullOrWhiteSpace( logFilePath );
 37
 38
 1639        services.AddHostedService<SerilogTracingListener>();
 40
 41
 1642        return services.AddSerilog( configuration =>
 1643        {
 1644            if ( isProduction ) { configuration.MinimumLevel.Information(); }
 1645            else { configuration.MinimumLevel.Verbose(); }
 1646
 1647
 1648            configuration.Enrich.WithDemystifiedStackTraces()
 1649                         .Enrich.WithAssemblyInformationalVersion()
 1650                         .Enrich.WithClientIp()
 1651                         .Enrich.WithCorrelationId()
 1652                         .Enrich.WithMachineName()
 1653                         .Enrich.WithMemoryUsage()
 1654                         .Enrich.WithProcessId()
 1655                         .Enrich.WithProcessName();
 1656
 1657
 1658            if ( !isProduction )
 1659            {
 1660                configuration.WriteTo.Async( sink => sink.Console( formatProvider: CultureInfo.InvariantCulture ), block
 1661                             .WriteTo.Async( sink => sink.Debug( formatProvider: CultureInfo.InvariantCulture ), blockWh
 1662            }
 1663
 1664
 1665            configuration.WriteTo.Async( sink => sink.File( Path.Combine( logFilePath, LogFileName ),
 1666                                                            rollingInterval: RollingInterval.Day,
 1667                                                            fileSizeLimitBytes: FileSizeLimitBytes,
 1668                                                            shared: true,
 1669                                                            formatProvider: CultureInfo.InvariantCulture ),
 1670                                         blockWhenFull: true );
 1671        } );
 72    }
 73}