Skip to content

More LoggerMessageAttribute #35368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions src/Middleware/HostFiltering/src/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.HostFiltering
{
internal static class LoggerExtensions
internal static partial class LoggerExtensions
{
private static readonly LogDefineOptions SkipEnabledCheckLogOptions = new() { SkipEnabledCheck = true };
[LoggerMessage(0, LogLevel.Debug, "Wildcard detected, all requests with hosts will be allowed.", EventName = "WildcardDetected")]
public static partial void WildcardDetected(this ILogger logger);

private static readonly Action<ILogger, Exception?> _wildcardDetected =
LoggerMessage.Define(LogLevel.Debug, new EventId(0, "WildcardDetected"), "Wildcard detected, all requests with hosts will be allowed.");
[LoggerMessage(1, LogLevel.Debug, "Allowed hosts: {Hosts}", EventName = "AllowedHosts", SkipEnabledCheck = true)]
public static partial void AllowedHosts(this ILogger logger, string hosts);

private static readonly Action<ILogger, string, Exception?> _allowedHosts =
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "AllowedHosts"), "Allowed hosts: {Hosts}", SkipEnabledCheckLogOptions);
[LoggerMessage(2, LogLevel.Trace, "All hosts are allowed.", EventName = "AllHostsAllowed")]
public static partial void AllHostsAllowed(this ILogger logger);

private static readonly Action<ILogger, Exception?> _allHostsAllowed =
LoggerMessage.Define(LogLevel.Trace, new EventId(2, "AllHostsAllowed"), "All hosts are allowed.");
[LoggerMessage(3, LogLevel.Information, "{Protocol} request rejected due to missing or empty host header.", EventName = "RequestRejectedMissingHost")]
public static partial void RequestRejectedMissingHost(this ILogger logger, string protocol);

private static readonly Action<ILogger, string, Exception?> _requestRejectedMissingHost =
LoggerMessage.Define<string>(LogLevel.Information, new EventId(3, "RequestRejectedMissingHost"), "{Protocol} request rejected due to missing or empty host header.");
[LoggerMessage(4, LogLevel.Debug, "{Protocol} request allowed with missing or empty host header.", EventName = "RequestAllowedMissingHost")]
public static partial void RequestAllowedMissingHost(this ILogger logger, string protocol);

private static readonly Action<ILogger, string, Exception?> _requestAllowedMissingHost =
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(4, "RequestAllowedMissingHost"), "{Protocol} request allowed with missing or empty host header.");
[LoggerMessage(5, LogLevel.Trace, "The host '{Host}' matches an allowed host.", EventName = "AllowedHostMatched")]
public static partial void AllowedHostMatched(this ILogger logger, string host);

private static readonly Action<ILogger, string, Exception?> _allowedHostMatched =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(5, "AllowedHostMatched"), "The host '{Host}' matches an allowed host.");

private static readonly Action<ILogger, string, Exception?> _noAllowedHostMatched =
LoggerMessage.Define<string>(LogLevel.Information, new EventId(6, "NoAllowedHostMatched"), "The host '{Host}' does not match an allowed host.");

public static void WildcardDetected(this ILogger logger) => _wildcardDetected(logger, null);
public static void AllowedHosts(this ILogger logger, string allowedHosts) => _allowedHosts(logger, allowedHosts, null);
public static void AllHostsAllowed(this ILogger logger) => _allHostsAllowed(logger, null);
public static void RequestRejectedMissingHost(this ILogger logger, string protocol) => _requestRejectedMissingHost(logger, protocol, null);
public static void RequestAllowedMissingHost(this ILogger logger, string protocol) => _requestAllowedMissingHost(logger, protocol, null);
public static void AllowedHostMatched(this ILogger logger, string host) => _allowedHostMatched(logger, host, null);
public static void NoAllowedHostMatched(this ILogger logger, string host) => _noAllowedHostMatched(logger, host, null);
[LoggerMessage(6, LogLevel.Information, "The host '{Host}' does not match an allowed host.", EventName = "NoAllowedHostMatched")]
public static partial void NoAllowedHostMatched(this ILogger logger, string host);
}
}
42 changes: 7 additions & 35 deletions src/Middleware/HttpsPolicy/src/HstsLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,15 @@

namespace Microsoft.AspNetCore.HttpsPolicy
{
internal static class HstsLoggingExtensions
internal static partial class HstsLoggingExtensions
{
private static readonly Action<ILogger, Exception?> _notSecure;
private static readonly Action<ILogger, string, Exception?> _excludedHost;
private static readonly Action<ILogger, Exception?> _addingHstsHeader;
[LoggerMessage(1, LogLevel.Debug, "The request is insecure. Skipping HSTS header.", EventName = "NotSecure")]
public static partial void SkippingInsecure(this ILogger logger);

static HstsLoggingExtensions()
{
_notSecure = LoggerMessage.Define(
LogLevel.Debug,
new EventId(1, "NotSecure"),
"The request is insecure. Skipping HSTS header.");
[LoggerMessage(2, LogLevel.Debug, "The host '{host}' is excluded. Skipping HSTS header.", EventName = "ExcludedHost")]
public static partial void SkippingExcludedHost(this ILogger logger, string host);

_excludedHost = LoggerMessage.Define<string>(
LogLevel.Debug,
new EventId(2, "ExcludedHost"),
"The host '{host}' is excluded. Skipping HSTS header.");

_addingHstsHeader = LoggerMessage.Define(
LogLevel.Trace,
new EventId(3, "AddingHstsHeader"),
"Adding HSTS header to response.");
}

public static void SkippingInsecure(this ILogger logger)
{
_notSecure(logger, null);
}

public static void SkippingExcludedHost(this ILogger logger, string host)
{
_excludedHost(logger, host, null);
}

public static void AddingHstsHeader(this ILogger logger)
{
_addingHstsHeader(logger, null);
}
[LoggerMessage(3, LogLevel.Trace, "Adding HSTS header to response.", EventName = "AddingHstsHeader")]
public static partial void AddingHstsHeader(this ILogger logger);
}
}
54 changes: 9 additions & 45 deletions src/Middleware/HttpsPolicy/src/HttpsLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,18 @@

namespace Microsoft.AspNetCore.HttpsPolicy
{
internal static class HttpsLoggingExtensions
internal static partial class HttpsLoggingExtensions
{
private static readonly Action<ILogger, string, Exception?> _redirectingToHttps;
private static readonly Action<ILogger, int, Exception?> _portLoadedFromConfig;
private static readonly Action<ILogger, Exception?> _failedToDeterminePort;
private static readonly Action<ILogger, int, Exception?> _portFromServer;
[LoggerMessage(1, LogLevel.Debug, "Redirecting to '{redirect}'.", EventName = "RedirectingToHttps")]
public static partial void RedirectingToHttps(this ILogger logger, string redirect);

static HttpsLoggingExtensions()
{
_redirectingToHttps = LoggerMessage.Define<string>(
LogLevel.Debug,
new EventId(1, "RedirectingToHttps"),
"Redirecting to '{redirect}'.");
[LoggerMessage(2, LogLevel.Debug, "Https port '{port}' loaded from configuration.", EventName = "PortLoadedFromConfig")]
public static partial void PortLoadedFromConfig(this ILogger logger, int port);

_portLoadedFromConfig = LoggerMessage.Define<int>(
LogLevel.Debug,
new EventId(2, "PortLoadedFromConfig"),
"Https port '{port}' loaded from configuration.");
[LoggerMessage(3, LogLevel.Warning, "Failed to determine the https port for redirect.", EventName = "FailedToDeterminePort")]
public static partial void FailedToDeterminePort(this ILogger logger);

_failedToDeterminePort = LoggerMessage.Define(
LogLevel.Warning,
new EventId(3, "FailedToDeterminePort"),
"Failed to determine the https port for redirect.");

_portFromServer = LoggerMessage.Define<int>(
LogLevel.Debug,
new EventId(5, "PortFromServer"),
"Https port '{httpsPort}' discovered from server endpoints.");
}

public static void RedirectingToHttps(this ILogger logger, string redirect)
{
_redirectingToHttps(logger, redirect, null);
}

public static void PortLoadedFromConfig(this ILogger logger, int port)
{
_portLoadedFromConfig(logger, port, null);
}

public static void FailedToDeterminePort(this ILogger logger)
{
_failedToDeterminePort(logger, null);
}

public static void PortFromServer(this ILogger logger, int port)
{
_portFromServer(logger, port, null);
}
[LoggerMessage(5, LogLevel.Debug, "Https port '{httpsPort}' discovered from server endpoints.", EventName = "PortFromServer")]
public static partial void PortFromServer(this ILogger logger, int httpsPort);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,13 @@

namespace Microsoft.AspNetCore.Localization
{
internal static class RequestCultureProviderLoggerExtensions
internal static partial class RequestCultureProviderLoggerExtensions
{
private static readonly Action<ILogger, string, IList<StringSegment>, Exception?> _unsupportedCulture;
private static readonly Action<ILogger, string, IList<StringSegment>, Exception?> _unsupportedUICulture;

static RequestCultureProviderLoggerExtensions()
{
_unsupportedCulture = LoggerMessage.Define<string, IList<StringSegment>>(
LogLevel.Debug,
new EventId (1, "UnsupportedCulture"),
"{requestCultureProvider} returned the following unsupported cultures '{cultures}'.");
_unsupportedUICulture = LoggerMessage.Define<string, IList<StringSegment>>(
LogLevel.Debug,
new EventId(2, "UnsupportedUICulture"),
"{requestCultureProvider} returned the following unsupported UI Cultures '{uiCultures}'.");
}
[LoggerMessage(1, LogLevel.Debug, "{requestCultureProvider} returned the following unsupported cultures '{cultures}'.", EventName = "UnsupportedCulture")]
public static partial void UnsupportedCultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> cultures);

public static void UnsupportedCultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> cultures)
{
_unsupportedCulture(logger, requestCultureProvider, cultures, null);
}

public static void UnsupportedUICultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> uiCultures)
{
_unsupportedUICulture(logger, requestCultureProvider, uiCultures, null);
}
[LoggerMessage(2, LogLevel.Debug, "{requestCultureProvider} returned the following unsupported UI Cultures '{uiCultures}'.", EventName = "UnsupportedUICulture")]
public static partial void UnsupportedUICultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> uiCultures);
}
}
Loading