< Summary

Information
Class: ProjectTemplate.Web.Diagnostics.RequestLoggingPrivacy
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Diagnostics/RequestLoggingPrivacy.cs
Line coverage
100%
Covered lines: 7
Uncovered lines: 0
Coverable lines: 7
Total lines: 39
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetLoggableRemoteIpAddress(...)90%1010100%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Diagnostics/RequestLoggingPrivacy.cs

#LineLine coverage
 1using Microsoft.Extensions.Options;
 2using ProjectTemplate.Web.Options;
 3
 4namespace ProjectTemplate.Web.Diagnostics;
 5
 6/// <summary>
 7/// Applies the request-logging privacy options to diagnostic log entries written outside the request-logging
 8/// middleware, such as rate-limit rejection and error-page log entries.
 9/// </summary>
 10/// <remarks>
 11/// <see cref="ApplicationRequestLoggingOptions.IncludeRemoteIpAddress"/> is the single application-wide decision about
 12/// whether client IP addresses are written to logs. Every log entry that would otherwise record the remote IP address
 13/// should obtain it through this class so the decision is honored consistently.
 14/// </remarks>
 15internal static class RequestLoggingPrivacy
 16{
 17    /// <summary>
 18    /// Gets the remote IP address to write to a log entry, or <see langword="null"/> when remote IP address logging is
 19    /// not enabled.
 20    /// </summary>
 21    /// <param name="httpContext">The current HTTP context.</param>
 22    /// <returns>
 23    /// The remote IP address when <see cref="ApplicationRequestLoggingOptions.IncludeRemoteIpAddress"/> is
 24    /// <see langword="true"/> and an address is available; otherwise <see langword="null"/>. When the options are not
 25    /// registered, the privacy-preserving default applies and <see langword="null"/> is returned.
 26    /// </returns>
 27    internal static string? GetLoggableRemoteIpAddress(HttpContext httpContext)
 28    {
 3429        ArgumentNullException.ThrowIfNull(httpContext);
 30
 3431        ApplicationRequestLoggingOptions? options = httpContext.RequestServices?
 3432            .GetService<IOptions<ApplicationRequestLoggingOptions>>()?
 3433            .Value;
 34
 3435        return options?.IncludeRemoteIpAddress == true
 3436            ? httpContext.Connection.RemoteIpAddress?.ToString()
 3437            : null;
 38    }
 39}