|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -using System; |
5 | 4 | using Microsoft.Extensions.Logging;
|
6 | 5 |
|
7 | 6 | namespace Microsoft.AspNetCore.HostFiltering
|
8 | 7 | {
|
9 |
| - internal static class LoggerExtensions |
| 8 | + internal static partial class LoggerExtensions |
10 | 9 | {
|
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); |
12 | 12 |
|
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); |
15 | 15 |
|
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); |
18 | 18 |
|
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); |
21 | 21 |
|
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); |
24 | 24 |
|
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); |
27 | 27 |
|
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); |
41 | 30 | }
|
42 | 31 | }
|
0 commit comments