Skip to content

Commit 61fc66c

Browse files
authored
More LoggerMessageAttribute (#35368)
* More LoggerMessageAttribute * src/Middleware * Some MVC projects
1 parent cddec67 commit 61fc66c

File tree

12 files changed

+216
-669
lines changed

12 files changed

+216
-669
lines changed
Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using Microsoft.Extensions.Logging;
65

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

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

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

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

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

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

28-
private static readonly Action<ILogger, string, Exception?> _allowedHostMatched =
29-
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(5, "AllowedHostMatched"), "The host '{Host}' matches an allowed host.");
30-
31-
private static readonly Action<ILogger, string, Exception?> _noAllowedHostMatched =
32-
LoggerMessage.Define<string>(LogLevel.Information, new EventId(6, "NoAllowedHostMatched"), "The host '{Host}' does not match an allowed host.");
33-
34-
public static void WildcardDetected(this ILogger logger) => _wildcardDetected(logger, null);
35-
public static void AllowedHosts(this ILogger logger, string allowedHosts) => _allowedHosts(logger, allowedHosts, null);
36-
public static void AllHostsAllowed(this ILogger logger) => _allHostsAllowed(logger, null);
37-
public static void RequestRejectedMissingHost(this ILogger logger, string protocol) => _requestRejectedMissingHost(logger, protocol, null);
38-
public static void RequestAllowedMissingHost(this ILogger logger, string protocol) => _requestAllowedMissingHost(logger, protocol, null);
39-
public static void AllowedHostMatched(this ILogger logger, string host) => _allowedHostMatched(logger, host, null);
40-
public static void NoAllowedHostMatched(this ILogger logger, string host) => _noAllowedHostMatched(logger, host, null);
28+
[LoggerMessage(6, LogLevel.Information, "The host '{Host}' does not match an allowed host.", EventName = "NoAllowedHostMatched")]
29+
public static partial void NoAllowedHostMatched(this ILogger logger, string host);
4130
}
4231
}

src/Middleware/HttpsPolicy/src/HstsLoggingExtensions.cs

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,15 @@
66

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

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

22-
_excludedHost = LoggerMessage.Define<string>(
23-
LogLevel.Debug,
24-
new EventId(2, "ExcludedHost"),
25-
"The host '{host}' is excluded. Skipping HSTS header.");
26-
27-
_addingHstsHeader = LoggerMessage.Define(
28-
LogLevel.Trace,
29-
new EventId(3, "AddingHstsHeader"),
30-
"Adding HSTS header to response.");
31-
}
32-
33-
public static void SkippingInsecure(this ILogger logger)
34-
{
35-
_notSecure(logger, null);
36-
}
37-
38-
public static void SkippingExcludedHost(this ILogger logger, string host)
39-
{
40-
_excludedHost(logger, host, null);
41-
}
42-
43-
public static void AddingHstsHeader(this ILogger logger)
44-
{
45-
_addingHstsHeader(logger, null);
46-
}
17+
[LoggerMessage(3, LogLevel.Trace, "Adding HSTS header to response.", EventName = "AddingHstsHeader")]
18+
public static partial void AddingHstsHeader(this ILogger logger);
4719
}
4820
}

src/Middleware/HttpsPolicy/src/HttpsLoggingExtensions.cs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,18 @@
66

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

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

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

28-
_failedToDeterminePort = LoggerMessage.Define(
29-
LogLevel.Warning,
30-
new EventId(3, "FailedToDeterminePort"),
31-
"Failed to determine the https port for redirect.");
32-
33-
_portFromServer = LoggerMessage.Define<int>(
34-
LogLevel.Debug,
35-
new EventId(5, "PortFromServer"),
36-
"Https port '{httpsPort}' discovered from server endpoints.");
37-
}
38-
39-
public static void RedirectingToHttps(this ILogger logger, string redirect)
40-
{
41-
_redirectingToHttps(logger, redirect, null);
42-
}
43-
44-
public static void PortLoadedFromConfig(this ILogger logger, int port)
45-
{
46-
_portLoadedFromConfig(logger, port, null);
47-
}
48-
49-
public static void FailedToDeterminePort(this ILogger logger)
50-
{
51-
_failedToDeterminePort(logger, null);
52-
}
53-
54-
public static void PortFromServer(this ILogger logger, int port)
55-
{
56-
_portFromServer(logger, port, null);
57-
}
20+
[LoggerMessage(5, LogLevel.Debug, "Https port '{httpsPort}' discovered from server endpoints.", EventName = "PortFromServer")]
21+
public static partial void PortFromServer(this ILogger logger, int httpsPort);
5822
}
5923
}

src/Middleware/Localization/src/RequestCultureProviderLoggerExtensions.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,13 @@
88

99
namespace Microsoft.AspNetCore.Localization
1010
{
11-
internal static class RequestCultureProviderLoggerExtensions
11+
internal static partial class RequestCultureProviderLoggerExtensions
1212
{
13-
private static readonly Action<ILogger, string, IList<StringSegment>, Exception?> _unsupportedCulture;
14-
private static readonly Action<ILogger, string, IList<StringSegment>, Exception?> _unsupportedUICulture;
1513

16-
static RequestCultureProviderLoggerExtensions()
17-
{
18-
_unsupportedCulture = LoggerMessage.Define<string, IList<StringSegment>>(
19-
LogLevel.Debug,
20-
new EventId (1, "UnsupportedCulture"),
21-
"{requestCultureProvider} returned the following unsupported cultures '{cultures}'.");
22-
_unsupportedUICulture = LoggerMessage.Define<string, IList<StringSegment>>(
23-
LogLevel.Debug,
24-
new EventId(2, "UnsupportedUICulture"),
25-
"{requestCultureProvider} returned the following unsupported UI Cultures '{uiCultures}'.");
26-
}
14+
[LoggerMessage(1, LogLevel.Debug, "{requestCultureProvider} returned the following unsupported cultures '{cultures}'.", EventName = "UnsupportedCulture")]
15+
public static partial void UnsupportedCultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> cultures);
2716

28-
public static void UnsupportedCultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> cultures)
29-
{
30-
_unsupportedCulture(logger, requestCultureProvider, cultures, null);
31-
}
32-
33-
public static void UnsupportedUICultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> uiCultures)
34-
{
35-
_unsupportedUICulture(logger, requestCultureProvider, uiCultures, null);
36-
}
17+
[LoggerMessage(2, LogLevel.Debug, "{requestCultureProvider} returned the following unsupported UI Cultures '{uiCultures}'.", EventName = "UnsupportedUICulture")]
18+
public static partial void UnsupportedUICultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> uiCultures);
3719
}
3820
}

0 commit comments

Comments
 (0)