-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Soft enable CA1810 #33619
Soft enable CA1810 #33619
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,21 +163,16 @@ protected override void HandleException(Exception exception) | |
|
||
private static class Log | ||
{ | ||
private static readonly Action<ILogger, string, Exception> _unhandledExceptionRenderingComponent; | ||
private static readonly Action<ILogger, string, Exception> _unhandledExceptionRenderingComponent = LoggerMessage.Define<string>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or maybe something like below? [LoggerMessage(EventId = EventIds.UnhandledExceptionRenderingComponent, Level = LogLevel.Critical, Message = "Unhandled exception rendering component: {Message}")]
public static partial void UnhandledExceptionRenderingComponent(ILogger logger, string message); |
||
LogLevel.Critical, | ||
EventIds.UnhandledExceptionRenderingComponent, | ||
"Unhandled exception rendering component: {Message}"); | ||
|
||
private static class EventIds | ||
{ | ||
public static readonly EventId UnhandledExceptionRenderingComponent = new EventId(100, "ExceptionRenderingComponent"); | ||
} | ||
|
||
static Log() | ||
{ | ||
_unhandledExceptionRenderingComponent = LoggerMessage.Define<string>( | ||
LogLevel.Critical, | ||
EventIds.UnhandledExceptionRenderingComponent, | ||
"Unhandled exception rendering component: {Message}"); | ||
} | ||
|
||
public static void UnhandledExceptionRenderingComponent(ILogger logger, Exception exception) | ||
{ | ||
_unhandledExceptionRenderingComponent( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,28 +11,18 @@ | |
|
||
namespace Microsoft.AspNetCore.Hosting | ||
{ | ||
internal static class HostingLoggerExtensions | ||
internal static partial class HostingLoggerExtensions | ||
{ | ||
private static readonly Action<ILogger, string, Exception?> _startupAssemblyLoaded = | ||
LoggerMessage.Define<string>(LogLevel.Debug, LoggerEventIds.HostingStartupAssemblyLoaded, "Loaded hosting startup assembly {assemblyName}", skipEnabledCheck: true); | ||
|
||
private static readonly Action<ILogger, string, Exception?> _listeningOnAddress = | ||
LoggerMessage.Define<string>(LogLevel.Information, LoggerEventIds.ServerListeningOnAddresses, "Now listening on: {address}"); | ||
|
||
public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext) | ||
{ | ||
return logger.BeginScope(new HostingLogScope(httpContext)); | ||
} | ||
|
||
public static void ListeningOnAddress(this ILogger logger, string address) | ||
{ | ||
_listeningOnAddress(logger, address, null); | ||
} | ||
[LoggerMessage(EventId = LoggerEventIds.ServerListeningOnAddresses, EventName = "ServerListeningOnAddresses", Level = LogLevel.Information, Message = "Now listening on: {address}")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, when you don't specify EventName, it would take the method name as the event name for the log method. Instead of setting EventName, I would rename this method from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to be explicit about the event name. It is easy for someone to unknowingly change the event name by renaming a method. That would introduce a breaking change. That is the reason why we have strings today instead of using |
||
public static partial void ListeningOnAddress(this ILogger logger, string address); | ||
|
||
public static void StartupAssemblyLoaded(this ILogger logger, string assemblyName) | ||
{ | ||
_startupAssemblyLoaded(logger, assemblyName, null); | ||
} | ||
[LoggerMessage(EventId = LoggerEventIds.HostingStartupAssemblyLoaded, EventName = "HostingStartupAssemblyLoaded", Level = LogLevel.Debug, Message = "Loaded hosting startup assembly {assemblyName}")] | ||
public static partial void StartupAssemblyLoaded(this ILogger logger, string assemblyName); | ||
|
||
public static void ApplicationError(this ILogger logger, Exception exception) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,23 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNetCore.Hosting | ||
{ | ||
internal static class LoggerEventIds | ||
{ | ||
public static readonly EventId RequestStarting = new EventId(1, "RequestStarting"); | ||
public static readonly EventId RequestFinished = new EventId(2, "RequestFinished"); | ||
public static readonly EventId Starting = new EventId(3, "Starting"); | ||
public static readonly EventId Started = new EventId(4, "Started"); | ||
public static readonly EventId Shutdown = new EventId(5, "Shutdown"); | ||
public static readonly EventId ApplicationStartupException = new EventId(6, "ApplicationStartupException"); | ||
public static readonly EventId ApplicationStoppingException = new EventId(7, "ApplicationStoppingException"); | ||
public static readonly EventId ApplicationStoppedException = new EventId(8, "ApplicationStoppedException"); | ||
public static readonly EventId HostedServiceStartException = new EventId(9, "HostedServiceStartException"); | ||
public static readonly EventId HostedServiceStopException = new EventId(10, "HostedServiceStopException"); | ||
public static readonly EventId HostingStartupAssemblyException = new EventId(11, "HostingStartupAssemblyException"); | ||
public static readonly EventId ServerShutdownException = new EventId(12, "ServerShutdownException"); | ||
public static readonly EventId HostingStartupAssemblyLoaded = new EventId(13, "HostingStartupAssemblyLoaded"); | ||
public static readonly EventId ServerListeningOnAddresses = new EventId(14, "ServerListeningOnAddresses"); | ||
public const int RequestStarting = 1; | ||
public const int RequestFinished = 2; | ||
public const int Starting = 3; | ||
public const int Started = 4; | ||
public const int Shutdown = 5; | ||
public const int ApplicationStartupException = 6; | ||
public const int ApplicationStoppingException = 7; | ||
public const int ApplicationStoppedException = 8; | ||
public const int HostedServiceStartException = 9; | ||
public const int HostedServiceStopException = 10; | ||
public const int HostingStartupAssemblyException = 11; | ||
public const int ServerShutdownException = 12; | ||
public const int HostingStartupAssemblyLoaded = 13; | ||
public const int ServerListeningOnAddresses = 14; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Http | |||||
/// <summary> | ||||||
/// Creates <see cref="RequestDelegate"/> implementations from <see cref="Delegate"/> request handlers. | ||||||
/// </summary> | ||||||
public static class RequestDelegateFactory | ||||||
public static partial class RequestDelegateFactory | ||||||
{ | ||||||
private static readonly MethodInfo ExecuteTaskOfTMethod = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteTask), BindingFlags.NonPublic | BindingFlags.Static)!; | ||||||
private static readonly MethodInfo ExecuteTaskOfStringMethod = typeof(RequestDelegateFactory).GetMethod(nameof(ExecuteTaskOfString), BindingFlags.NonPublic | BindingFlags.Static)!; | ||||||
|
@@ -809,37 +809,25 @@ private class FactoryContext | |||||
public List<(ParameterExpression, Expression)> TryParseParams { get; } = new(); | ||||||
} | ||||||
|
||||||
private static class Log | ||||||
private static partial class Log | ||||||
{ | ||||||
private static readonly Action<ILogger, Exception> _requestBodyIOException = LoggerMessage.Define( | ||||||
LogLevel.Debug, | ||||||
new EventId(1, "RequestBodyIOException"), | ||||||
"Reading the request body failed with an IOException."); | ||||||
public static void RequestBodyIOException(HttpContext httpContext, Exception exception) | ||||||
=> RequestBodyIOException(GetLogger(httpContext), exception); | ||||||
|
||||||
private static readonly Action<ILogger, Exception> _requestBodyInvalidDataException = LoggerMessage.Define( | ||||||
LogLevel.Debug, | ||||||
new EventId(2, "RequestBodyInvalidDataException"), | ||||||
"Reading the request body failed with an InvalidDataException."); | ||||||
|
||||||
private static readonly Action<ILogger, string, string, string, Exception?> _parameterBindingFailed = LoggerMessage.Define<string, string, string>( | ||||||
LogLevel.Debug, | ||||||
new EventId(3, "ParamaterBindingFailed"), | ||||||
@"Failed to bind parameter ""{ParameterType} {ParameterName}"" from ""{SourceValue}""."); | ||||||
|
||||||
public static void RequestBodyIOException(HttpContext httpContext, IOException exception) | ||||||
{ | ||||||
_requestBodyIOException(GetLogger(httpContext), exception); | ||||||
} | ||||||
[LoggerMessage(EventId = 1, EventName = "RequestBodyIOException", Level = LogLevel.Debug, Message = "Reading the request body failed with an IOException.")] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
public static partial void RequestBodyIOException(ILogger logger, Exception exception); | ||||||
|
||||||
public static void RequestBodyInvalidDataException(HttpContext httpContext, InvalidDataException exception) | ||||||
{ | ||||||
_requestBodyInvalidDataException(GetLogger(httpContext), exception); | ||||||
} | ||||||
=> RequestBodyInvalidDataException(GetLogger(httpContext), exception); | ||||||
|
||||||
[LoggerMessage(EventId = 2, EventName = "RequestBodyInvalidDataException", Level = LogLevel.Debug, Message = "Reading the request body failed with an InvalidDataException.")] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
public static partial void RequestBodyInvalidDataException(ILogger logger, InvalidDataException exception); | ||||||
|
||||||
public static void ParameterBindingFailed(HttpContext httpContext, string parameterTypeName, string parameterName, string sourceValue) | ||||||
{ | ||||||
_parameterBindingFailed(GetLogger(httpContext), parameterTypeName, parameterName, sourceValue, null); | ||||||
} | ||||||
=> ParameterBindingFailed(GetLogger(httpContext), parameterTypeName, parameterName, sourceValue); | ||||||
|
||||||
[LoggerMessage(EventId = 3, EventName = "ParamaterBindingFailed", Level = LogLevel.Debug, Message = @"Failed to bind parameter ""{ParameterType} {ParameterName}"" from ""{SourceValue}"".")] | ||||||
public static partial void ParameterBindingFailed(ILogger logger, string parameterType, string parameterName, string sourceValue); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
||||||
private static ILogger GetLogger(HttpContext httpContext) | ||||||
{ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Http | |
/// <summary> | ||
/// A wrapper for the response Set-Cookie header. | ||
/// </summary> | ||
internal class ResponseCookies : IResponseCookies | ||
internal sealed partial class ResponseCookies : IResponseCookies | ||
{ | ||
internal const string EnableCookieNameEncoding = "Microsoft.AspNetCore.Http.EnableCookieNameEncoding"; | ||
internal bool _enableCookieNameEncoding = AppContext.TryGetSwitch(EnableCookieNameEncoding, out var enabled) && enabled; | ||
|
@@ -212,17 +212,10 @@ public void Delete(string key, CookieOptions options) | |
}); | ||
} | ||
|
||
private static class Log | ||
private static partial class Log | ||
{ | ||
private static readonly Action<ILogger, string, Exception?> _samesiteNotSecure = LoggerMessage.Define<string>( | ||
LogLevel.Warning, | ||
EventIds.SameSiteNotSecure, | ||
"The cookie '{name}' has set 'SameSite=None' and must also set 'Secure'."); | ||
|
||
public static void SameSiteCookieNotSecure(ILogger logger, string name) | ||
{ | ||
_samesiteNotSecure(logger, name, null); | ||
} | ||
[LoggerMessage(EventId = 1, EventName = "SameSiteNotSecure", Level = LogLevel.Warning, Message = "The cookie '{name}' has set 'SameSite=None' and must also set 'Secure'.")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you don't set EventName explicitly the generator uses the log method's name as the event name. So you if you prefer, just remove |
||
public static partial void SameSiteCookieNotSecure(ILogger logger, string name); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will simply cause conflicts or be overwritten the next time we get versions from dotnet/runtime. Suggest retrying after #33560 is in.