|
5 | 5 |
|
6 | 6 | namespace Microsoft.AspNetCore.Antiforgery;
|
7 | 7 |
|
8 |
| -internal static class AntiforgeryLoggerExtensions |
| 8 | +internal static partial class AntiforgeryLoggerExtensions |
9 | 9 | {
|
10 |
| - private static readonly Action<ILogger, Exception?> _failedToDeserialzeTokens; |
11 |
| - private static readonly Action<ILogger, string, Exception?> _validationFailed; |
12 |
| - private static readonly Action<ILogger, Exception?> _validated; |
13 |
| - private static readonly Action<ILogger, string?, Exception?> _missingCookieToken; |
14 |
| - private static readonly Action<ILogger, string, string?, Exception?> _missingRequestToken; |
15 |
| - private static readonly Action<ILogger, Exception?> _newCookieToken; |
16 |
| - private static readonly Action<ILogger, Exception?> _reusedCookieToken; |
17 |
| - private static readonly Action<ILogger, Exception?> _tokenDeserializeException; |
18 |
| - private static readonly Action<ILogger, Exception?> _responseCacheHeadersOverridenToNoCache; |
| 10 | + [LoggerMessage(1, LogLevel.Warning, "Antiforgery validation failed with message '{Message}'.", EventName = "ValidationFailed")] |
| 11 | + public static partial void ValidationFailed(this ILogger logger, string message); |
19 | 12 |
|
20 |
| - static AntiforgeryLoggerExtensions() |
21 |
| - { |
22 |
| - _validationFailed = LoggerMessage.Define<string>( |
23 |
| - LogLevel.Warning, |
24 |
| - new EventId(1, "ValidationFailed"), |
25 |
| - "Antiforgery validation failed with message '{Message}'."); |
26 |
| - _validated = LoggerMessage.Define( |
27 |
| - LogLevel.Debug, |
28 |
| - new EventId(2, "Validated"), |
29 |
| - "Antiforgery successfully validated a request."); |
30 |
| - _missingCookieToken = LoggerMessage.Define<string?>( |
31 |
| - LogLevel.Warning, |
32 |
| - new EventId(3, "MissingCookieToken"), |
33 |
| - "The required antiforgery cookie '{CookieName}' is not present."); |
34 |
| - _missingRequestToken = LoggerMessage.Define<string, string?>( |
35 |
| - LogLevel.Warning, |
36 |
| - new EventId(4, "MissingRequestToken"), |
37 |
| - "The required antiforgery request token was not provided in either form field '{FormFieldName}' " |
38 |
| - + "or header '{HeaderName}'."); |
39 |
| - _newCookieToken = LoggerMessage.Define( |
40 |
| - LogLevel.Debug, |
41 |
| - new EventId(5, "NewCookieToken"), |
42 |
| - "A new antiforgery cookie token was created."); |
43 |
| - _reusedCookieToken = LoggerMessage.Define( |
44 |
| - LogLevel.Debug, |
45 |
| - new EventId(6, "ReusedCookieToken"), |
46 |
| - "An antiforgery cookie token was reused."); |
47 |
| - _tokenDeserializeException = LoggerMessage.Define( |
48 |
| - LogLevel.Error, |
49 |
| - new EventId(7, "TokenDeserializeException"), |
50 |
| - "An exception was thrown while deserializing the token."); |
51 |
| - _responseCacheHeadersOverridenToNoCache = LoggerMessage.Define( |
52 |
| - LogLevel.Warning, |
53 |
| - new EventId(8, "ResponseCacheHeadersOverridenToNoCache"), |
54 |
| - "The 'Cache-Control' and 'Pragma' headers have been overridden and set to 'no-cache, no-store' and " + |
55 |
| - "'no-cache' respectively to prevent caching of this response. Any response that uses antiforgery " + |
56 |
| - "should not be cached."); |
57 |
| - _failedToDeserialzeTokens = LoggerMessage.Define( |
58 |
| - LogLevel.Debug, |
59 |
| - new EventId(9, "FailedToDeserialzeTokens"), |
60 |
| - "Failed to deserialize antiforgery tokens."); |
61 |
| - } |
62 |
| - |
63 |
| - public static void ValidationFailed(this ILogger logger, string message) |
64 |
| - { |
65 |
| - _validationFailed(logger, message, null); |
66 |
| - } |
| 13 | + [LoggerMessage(2, LogLevel.Debug, "Antiforgery successfully validated a request.", EventName = "Validated")] |
| 14 | + public static partial void ValidatedAntiforgeryToken(this ILogger logger); |
67 | 15 |
|
68 |
| - public static void ValidatedAntiforgeryToken(this ILogger logger) |
69 |
| - { |
70 |
| - _validated(logger, null); |
71 |
| - } |
| 16 | + [LoggerMessage(3, LogLevel.Warning, "The required antiforgery cookie '{CookieName}' is not present.", EventName = "MissingCookieToken")] |
| 17 | + public static partial void MissingCookieToken(this ILogger logger, string? cookieName); |
72 | 18 |
|
73 |
| - public static void MissingCookieToken(this ILogger logger, string? cookieName) |
74 |
| - { |
75 |
| - _missingCookieToken(logger, cookieName, null); |
76 |
| - } |
| 19 | + [LoggerMessage(4, LogLevel.Warning, "The required antiforgery request token was not provided in either form field '{FormFieldName}' " |
| 20 | + + "or header '{HeaderName}'.", EventName = "MissingRequestToken")] |
| 21 | + public static partial void MissingRequestToken(this ILogger logger, string formFieldName, string? headerName); |
77 | 22 |
|
78 |
| - public static void MissingRequestToken(this ILogger logger, string formFieldName, string? headerName) |
79 |
| - { |
80 |
| - _missingRequestToken(logger, formFieldName, headerName, null); |
81 |
| - } |
| 23 | + [LoggerMessage(5, LogLevel.Debug, "A new antiforgery cookie token was created.", EventName = "NewCookieToken")] |
| 24 | + public static partial void NewCookieToken(this ILogger logger); |
82 | 25 |
|
83 |
| - public static void NewCookieToken(this ILogger logger) |
84 |
| - { |
85 |
| - _newCookieToken(logger, null); |
86 |
| - } |
| 26 | + [LoggerMessage(6, LogLevel.Debug, "An antiforgery cookie token was reused.", EventName = "ReusedCookieToken")] |
| 27 | + public static partial void ReusedCookieToken(this ILogger logger); |
87 | 28 |
|
88 |
| - public static void ReusedCookieToken(this ILogger logger) |
89 |
| - { |
90 |
| - _reusedCookieToken(logger, null); |
91 |
| - } |
| 29 | + [LoggerMessage(7, LogLevel.Error, "An exception was thrown while deserializing the token.", EventName = "TokenDeserializeException")] |
| 30 | + public static partial void TokenDeserializeException(this ILogger logger, Exception exception); |
92 | 31 |
|
93 |
| - public static void TokenDeserializeException(this ILogger logger, Exception exception) |
94 |
| - { |
95 |
| - _tokenDeserializeException(logger, exception); |
96 |
| - } |
97 |
| - |
98 |
| - public static void ResponseCacheHeadersOverridenToNoCache(this ILogger logger) |
99 |
| - { |
100 |
| - _responseCacheHeadersOverridenToNoCache(logger, null); |
101 |
| - } |
| 32 | + [LoggerMessage(8, LogLevel.Warning, "The 'Cache-Control' and 'Pragma' headers have been overridden and set to 'no-cache, no-store' and " + |
| 33 | + "'no-cache' respectively to prevent caching of this response. Any response that uses antiforgery " + |
| 34 | + "should not be cached.", EventName = "ResponseCacheHeadersOverridenToNoCache")] |
| 35 | + public static partial void ResponseCacheHeadersOverridenToNoCache(this ILogger logger); |
102 | 36 |
|
103 |
| - public static void FailedToDeserialzeTokens(this ILogger logger, Exception exception) |
104 |
| - { |
105 |
| - _failedToDeserialzeTokens(logger, exception); |
106 |
| - } |
| 37 | + [LoggerMessage(9, LogLevel.Debug, "Failed to deserialize antiforgery tokens.", EventName = "FailedToDeserialzeTokens")] |
| 38 | + public static partial void FailedToDeserialzeTokens(this ILogger logger, Exception exception); |
107 | 39 | }
|
0 commit comments