Skip to content

Commit bc1efeb

Browse files
authored
Update Mvc to use LoggerMessageAttribute (#38658)
Updates the Mvc project to use the LoggerMessageAttribute for logging generation. ## Description The following files contained duplicate log event ids which had to be renumbered: - MvcCoreLoggerExtensions - MvcRazorLoggerExtensions - MvcViewFeaturesLoggerExtensions - RazorRuntimeCompilationLoggerExtensions Also fixes a typo in a method name in RazorRuntimeCompilationLoggerExtensions: ViewCompilerInvalidingCompiledFile --> ViewCompilerInvalidatingCompiledFile Contributes to #32087
1 parent ac12b4c commit bc1efeb

File tree

46 files changed

+666
-1229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+666
-1229
lines changed

src/Mvc/Mvc.Core/src/Filters/DisableRequestSizeLimitFilter.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Http.Features;
@@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.Filters;
1010
/// A filter that sets <see cref="IHttpMaxRequestBodySizeFeature.MaxRequestBodySize"/>
1111
/// to <c>null</c>.
1212
/// </summary>
13-
internal class DisableRequestSizeLimitFilter : IAuthorizationFilter, IRequestSizePolicy
13+
internal partial class DisableRequestSizeLimitFilter : IAuthorizationFilter, IRequestSizePolicy
1414
{
1515
private readonly ILogger _logger;
1616

@@ -39,24 +39,39 @@ public void OnAuthorization(AuthorizationFilterContext context)
3939
var effectivePolicy = context.FindEffectivePolicy<IRequestSizePolicy>();
4040
if (effectivePolicy != null && effectivePolicy != this)
4141
{
42-
_logger.NotMostEffectiveFilter(GetType(), effectivePolicy.GetType(), typeof(IRequestSizePolicy));
42+
Log.NotMostEffectiveFilter(_logger, GetType(), effectivePolicy.GetType(), typeof(IRequestSizePolicy));
4343
return;
4444
}
4545

4646
var maxRequestBodySizeFeature = context.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>();
4747

4848
if (maxRequestBodySizeFeature == null)
4949
{
50-
_logger.FeatureNotFound();
50+
Log.FeatureNotFound(_logger);
5151
}
5252
else if (maxRequestBodySizeFeature.IsReadOnly)
5353
{
54-
_logger.FeatureIsReadOnly();
54+
Log.FeatureIsReadOnly(_logger);
5555
}
5656
else
5757
{
5858
maxRequestBodySizeFeature.MaxRequestBodySize = null;
59-
_logger.RequestBodySizeLimitDisabled();
59+
Log.RequestBodySizeLimitDisabled(_logger);
6060
}
6161
}
62+
63+
private static partial class Log
64+
{
65+
[LoggerMessage(1, LogLevel.Warning, "A request body size limit could not be applied. This server does not support the IHttpRequestBodySizeFeature.", EventName = "FeatureNotFound")]
66+
public static partial void FeatureNotFound(ILogger logger);
67+
68+
[LoggerMessage(2, LogLevel.Warning, "A request body size limit could not be applied. The IHttpRequestBodySizeFeature for the server is read-only.", EventName = "FeatureIsReadOnly")]
69+
public static partial void FeatureIsReadOnly(ILogger logger);
70+
71+
[LoggerMessage(3, LogLevel.Debug, "The request body size limit has been disabled.", EventName = "RequestBodySizeLimitDisabled")]
72+
public static partial void RequestBodySizeLimitDisabled(ILogger logger);
73+
74+
[LoggerMessage(4, LogLevel.Debug, "Execution of filter {OverriddenFilter} is preempted by filter {OverridingFilter} which is the most effective filter implementing policy {FilterPolicy}.", EventName = "NotMostEffectiveFilter")]
75+
public static partial void NotMostEffectiveFilter(ILogger logger, Type overriddenFilter, Type overridingFilter, Type filterPolicy);
76+
}
6277
}

src/Mvc/Mvc.Core/src/Filters/RequestFormLimitsFilter.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.Filters;
99
/// <summary>
1010
/// A filter that configures <see cref="FormOptions"/> for the current request.
1111
/// </summary>
12-
internal class RequestFormLimitsFilter : IAuthorizationFilter, IRequestFormLimitsPolicy
12+
internal partial class RequestFormLimitsFilter : IAuthorizationFilter, IRequestFormLimitsPolicy
1313
{
1414
private readonly ILogger _logger;
1515

@@ -30,7 +30,7 @@ public void OnAuthorization(AuthorizationFilterContext context)
3030
var effectivePolicy = context.FindEffectivePolicy<IRequestFormLimitsPolicy>();
3131
if (effectivePolicy != null && effectivePolicy != this)
3232
{
33-
_logger.NotMostEffectiveFilter(GetType(), effectivePolicy.GetType(), typeof(IRequestFormLimitsPolicy));
33+
Log.NotMostEffectiveFilter(_logger, GetType(), effectivePolicy.GetType(), typeof(IRequestFormLimitsPolicy));
3434
return;
3535
}
3636

@@ -41,11 +41,23 @@ public void OnAuthorization(AuthorizationFilterContext context)
4141
{
4242
// Request form has not been read yet, so set the limits
4343
features.Set<IFormFeature>(new FormFeature(context.HttpContext.Request, FormOptions));
44-
_logger.AppliedRequestFormLimits();
44+
Log.AppliedRequestFormLimits(_logger);
4545
}
4646
else
4747
{
48-
_logger.CannotApplyRequestFormLimits();
48+
Log.CannotApplyRequestFormLimits(_logger);
4949
}
5050
}
51+
52+
private static partial class Log
53+
{
54+
[LoggerMessage(1, LogLevel.Warning, "Unable to apply configured form options since the request form has already been read.", EventName = "CannotApplyRequestFormLimits")]
55+
public static partial void CannotApplyRequestFormLimits(ILogger logger);
56+
57+
[LoggerMessage(2, LogLevel.Debug, "Applied the configured form options on the current request.", EventName = "AppliedRequestFormLimits")]
58+
public static partial void AppliedRequestFormLimits(ILogger logger);
59+
60+
[LoggerMessage(4, LogLevel.Debug, "Execution of filter {OverriddenFilter} is preempted by filter {OverridingFilter} which is the most effective filter implementing policy {FilterPolicy}.", EventName = "NotMostEffectiveFilter")]
61+
public static partial void NotMostEffectiveFilter(ILogger logger, Type overriddenFilter, Type overridingFilter, Type filterPolicy);
62+
}
5163
}

src/Mvc/Mvc.Core/src/Filters/RequestSizeLimitFilter.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Mvc.Filters;
1111
/// A filter that sets the <see cref="IHttpMaxRequestBodySizeFeature.MaxRequestBodySize"/>
1212
/// to the specified <see cref="Bytes"/>.
1313
/// </summary>
14-
internal class RequestSizeLimitFilter : IAuthorizationFilter, IRequestSizePolicy
14+
internal partial class RequestSizeLimitFilter : IAuthorizationFilter, IRequestSizePolicy
1515
{
1616
private readonly ILogger _logger;
1717

@@ -41,24 +41,39 @@ public void OnAuthorization(AuthorizationFilterContext context)
4141
var effectivePolicy = context.FindEffectivePolicy<IRequestSizePolicy>();
4242
if (effectivePolicy != null && effectivePolicy != this)
4343
{
44-
_logger.NotMostEffectiveFilter(GetType(), effectivePolicy.GetType(), typeof(IRequestSizePolicy));
44+
Log.NotMostEffectiveFilter(_logger, GetType(), effectivePolicy.GetType(), typeof(IRequestSizePolicy));
4545
return;
4646
}
4747

4848
var maxRequestBodySizeFeature = context.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>();
4949

5050
if (maxRequestBodySizeFeature == null)
5151
{
52-
_logger.FeatureNotFound();
52+
Log.FeatureNotFound(_logger);
5353
}
5454
else if (maxRequestBodySizeFeature.IsReadOnly)
5555
{
56-
_logger.FeatureIsReadOnly();
56+
Log.FeatureIsReadOnly(_logger);
5757
}
5858
else
5959
{
6060
maxRequestBodySizeFeature.MaxRequestBodySize = Bytes;
61-
_logger.MaxRequestBodySizeSet(Bytes.ToString(CultureInfo.InvariantCulture));
61+
Log.MaxRequestBodySizeSet(_logger, Bytes.ToString(CultureInfo.InvariantCulture));
6262
}
6363
}
64+
65+
private static partial class Log
66+
{
67+
[LoggerMessage(1, LogLevel.Warning, "A request body size limit could not be applied. This server does not support the IHttpRequestBodySizeFeature.", EventName = "FeatureNotFound")]
68+
public static partial void FeatureNotFound(ILogger logger);
69+
70+
[LoggerMessage(2, LogLevel.Warning, "A request body size limit could not be applied. The IHttpRequestBodySizeFeature for the server is read-only.", EventName = "FeatureIsReadOnly")]
71+
public static partial void FeatureIsReadOnly(ILogger logger);
72+
73+
[LoggerMessage(3, LogLevel.Debug, "The maximum request body size has been set to {RequestSize}.", EventName = "MaxRequestBodySizeSet")]
74+
public static partial void MaxRequestBodySizeSet(ILogger logger, string requestSize);
75+
76+
[LoggerMessage(4, LogLevel.Debug, "Execution of filter {OverriddenFilter} is preempted by filter {OverridingFilter} which is the most effective filter implementing policy {FilterPolicy}.", EventName = "NotMostEffectiveFilter")]
77+
public static partial void NotMostEffectiveFilter(ILogger logger, Type overriddenFilter, Type overridingFilter, Type filterPolicy);
78+
}
6479
}

src/Mvc/Mvc.Core/src/Filters/ResponseCacheFilter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Mvc.Filters;
88
/// <summary>
99
/// An <see cref="IActionFilter"/> which sets the appropriate headers related to response caching.
1010
/// </summary>
11-
internal class ResponseCacheFilter : IActionFilter, IResponseCacheFilter
11+
internal partial class ResponseCacheFilter : IActionFilter, IResponseCacheFilter
1212
{
1313
private readonly ResponseCacheFilterExecutor _executor;
1414
private readonly ILogger _logger;
@@ -91,7 +91,7 @@ public void OnActionExecuting(ActionExecutingContext context)
9191
var effectivePolicy = context.FindEffectivePolicy<IResponseCacheFilter>();
9292
if (effectivePolicy != null && effectivePolicy != this)
9393
{
94-
_logger.NotMostEffectiveFilter(GetType(), effectivePolicy.GetType(), typeof(IResponseCacheFilter));
94+
Log.NotMostEffectiveFilter(_logger, GetType(), effectivePolicy.GetType(), typeof(IResponseCacheFilter));
9595
return;
9696
}
9797

@@ -102,4 +102,10 @@ public void OnActionExecuting(ActionExecutingContext context)
102102
public void OnActionExecuted(ActionExecutedContext context)
103103
{
104104
}
105+
106+
private static partial class Log
107+
{
108+
[LoggerMessage(4, LogLevel.Debug, "Execution of filter {OverriddenFilter} is preempted by filter {OverridingFilter} which is the most effective filter implementing policy {FilterPolicy}.", EventName = "NotMostEffectiveFilter")]
109+
public static partial void NotMostEffectiveFilter(ILogger logger, Type overriddenFilter, Type overridingFilter, Type filterPolicy);
110+
}
105111
}

src/Mvc/Mvc.Core/src/Formatters/FormatFilter.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters;
1414
/// A filter that will use the format value in the route data or query string to set the content type on an
1515
/// <see cref="ObjectResult"/> returned from an action.
1616
/// </summary>
17-
public class FormatFilter : IFormatFilter, IResourceFilter, IResultFilter
17+
public partial class FormatFilter : IFormatFilter, IResourceFilter, IResultFilter
1818
{
1919
private readonly MvcOptions _options;
2020
private readonly ILogger _logger;
@@ -82,7 +82,7 @@ public void OnResourceExecuting(ResourceExecutingContext context)
8282
var contentType = _options.FormatterMappings.GetMediaTypeMappingForFormat(format);
8383
if (contentType == null)
8484
{
85-
_logger.UnsupportedFormatFilterContentType(format);
85+
Log.UnsupportedFormatFilterContentType(_logger, format);
8686

8787
// no contentType exists for the format, return 404
8888
context.Result = new NotFoundResult();
@@ -100,7 +100,7 @@ public void OnResourceExecuting(ResourceExecutingContext context)
100100
// Check if support is adequate for requested media type.
101101
if (supportedMediaTypes.Count == 0)
102102
{
103-
_logger.ActionDoesNotExplicitlySpecifyContentTypes();
103+
Log.ActionDoesNotExplicitlySpecifyContentTypes(_logger);
104104
return;
105105
}
106106

@@ -163,7 +163,7 @@ public void OnResultExecuting(ResultExecutingContext context)
163163
if (objectResult.ContentTypes.Count == 1 ||
164164
!string.IsNullOrEmpty(context.HttpContext.Response.ContentType))
165165
{
166-
_logger.CannotApplyFormatFilterContentType(format);
166+
Log.CannotApplyFormatFilterContentType(_logger, format);
167167
return;
168168
}
169169

@@ -179,4 +179,16 @@ public void OnResultExecuting(ResultExecutingContext context)
179179
public void OnResultExecuted(ResultExecutedContext context)
180180
{
181181
}
182+
183+
private static partial class Log
184+
{
185+
[LoggerMessage(1, LogLevel.Debug, "Could not find a media type for the format '{FormatFilterContentType}'.", EventName = "UnsupportedFormatFilterContentType")]
186+
public static partial void UnsupportedFormatFilterContentType(ILogger logger, string formatFilterContentType);
187+
188+
[LoggerMessage(3, LogLevel.Debug, "Cannot apply content type '{FormatFilterContentType}' to the response as current action had explicitly set a preferred content type.", EventName = "CannotApplyFormatFilterContentType")]
189+
public static partial void CannotApplyFormatFilterContentType(ILogger logger, string formatFilterContentType);
190+
191+
[LoggerMessage(5, LogLevel.Debug, "Current action does not explicitly specify any content types for the response.", EventName = "ActionDoesNotExplicitlySpecifyContentTypes")]
192+
public static partial void ActionDoesNotExplicitlySpecifyContentTypes(ILogger logger);
193+
}
182194
}

src/Mvc/Mvc.Core/src/Infrastructure/ActionSelector.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure;
1515
/// <summary>
1616
/// A default <see cref="IActionSelector"/> implementation.
1717
/// </summary>
18-
internal class ActionSelector : IActionSelector
18+
internal partial class ActionSelector : IActionSelector
1919
{
2020
private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;
2121
private readonly ActionConstraintCache _actionConstraintCache;
@@ -108,8 +108,7 @@ public IReadOnlyList<ActionDescriptor> SelectCandidates(RouteContext context)
108108
var actionNames = string.Join(
109109
Environment.NewLine,
110110
finalMatches.Select(a => a.DisplayName));
111-
112-
_logger.AmbiguousActions(actionNames);
111+
Log.AmbiguousActions(_logger, actionNames);
113112

114113
var message = Resources.FormatDefaultActionSelector_AmbiguousActions(
115114
Environment.NewLine,
@@ -216,7 +215,8 @@ public IReadOnlyList<ActionDescriptor> SelectCandidates(RouteContext context)
216215
if (!constraint.Accept(constraintContext))
217216
{
218217
isMatch = false;
219-
_logger.ConstraintMismatch(
218+
Log.ConstraintMismatch(
219+
_logger,
220220
candidate.Action.DisplayName,
221221
candidate.Action.Id,
222222
constraint);
@@ -256,4 +256,13 @@ public IReadOnlyList<ActionDescriptor> SelectCandidates(RouteContext context)
256256
return EvaluateActionConstraintsCore(context, actionsWithoutConstraint, order);
257257
}
258258
}
259+
260+
private static partial class Log
261+
{
262+
[LoggerMessage(1, LogLevel.Error, "Request matched multiple actions resulting in ambiguity. Matching actions: {AmbiguousActions}", EventName = "AmbiguousActions")]
263+
public static partial void AmbiguousActions(ILogger logger, string ambiguousActions);
264+
265+
[LoggerMessage(2, LogLevel.Debug, "Action '{ActionName}' with id '{ActionId}' did not match the constraint '{ActionConstraint}'", EventName = "ConstraintMismatch")]
266+
public static partial void ConstraintMismatch(ILogger logger, string? actionName, string actionId, IActionConstraint actionConstraint);
267+
}
259268
}

src/Mvc/Mvc.Core/src/Infrastructure/ContentResultExecutor.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure;
1313
/// <summary>
1414
/// A <see cref="IActionResultExecutor{ContentResult}"/> that is responsible for <see cref="ContentResult"/>
1515
/// </summary>
16-
public class ContentResultExecutor : IActionResultExecutor<ContentResult>
16+
public partial class ContentResultExecutor : IActionResultExecutor<ContentResult>
1717
{
1818
private const string DefaultContentType = "text/plain; charset=utf-8";
1919
private readonly ILogger<ContentResultExecutor> _logger;
@@ -60,7 +60,7 @@ public virtual async Task ExecuteAsync(ActionContext context, ContentResult resu
6060
response.StatusCode = result.StatusCode.Value;
6161
}
6262

63-
_logger.ContentResultExecuting(resolvedContentType);
63+
Log.ContentResultExecuting(_logger, resolvedContentType);
6464

6565
if (result.Content != null)
6666
{
@@ -78,4 +78,10 @@ public virtual async Task ExecuteAsync(ActionContext context, ContentResult resu
7878
}
7979
}
8080
}
81+
82+
private static partial class Log
83+
{
84+
[LoggerMessage(1, LogLevel.Information, "Executing ContentResult with HTTP Response ContentType of {ContentType}", EventName = "ContentResultExecuting")]
85+
public static partial void ContentResultExecuting(ILogger logger, string contentType);
86+
}
8187
}

0 commit comments

Comments
 (0)