diff --git a/.editorconfig b/.editorconfig
index 332a3377e1cf..1066556ba091 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -91,6 +91,9 @@ dotnet_diagnostic.CA1802.severity = warning
# CA1805: Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = warning
+# CA1810: Do not initialize unnecessarily
+dotnet_diagnostic.CA1810.severity = suggestion
+
# CA1823: Remove empty Finalizers
dotnet_diagnostic.CA1821.severity = warning
diff --git a/eng/Versions.props b/eng/Versions.props
index 50eb391033fe..490858981176 100644
--- a/eng/Versions.props
+++ b/eng/Versions.props
@@ -95,7 +95,7 @@
6.0.0-preview.6.21314.2
6.0.0-preview.6.21314.2
6.0.0-preview.6.21314.2
- 6.0.0-preview.6.21314.2
+ 6.0.0-preview.7.21316.3
6.0.0-preview.6.21314.2
6.0.0-preview.6.21314.2
6.0.0-preview.6.21314.2
diff --git a/src/Components/Web/src/Forms/InputNumber.cs b/src/Components/Web/src/Forms/InputNumber.cs
index ec73e52164cc..6e84f715759e 100644
--- a/src/Components/Web/src/Forms/InputNumber.cs
+++ b/src/Components/Web/src/Forms/InputNumber.cs
@@ -14,9 +14,9 @@ namespace Microsoft.AspNetCore.Components.Forms
///
public class InputNumber<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TValue> : InputBase
{
- private readonly static string _stepAttributeValue; // Null by default, so only allows whole numbers as per HTML spec
+ private static readonly string _stepAttributeValue = GetStepAttributeValue();
- static InputNumber()
+ private static string GetStepAttributeValue()
{
// Unwrap Nullable, because InputBase already deals with the Nullable aspect
// of it for us. We will only get asked to parse the T for nonempty inputs.
@@ -28,7 +28,7 @@ static InputNumber()
targetType == typeof(double) ||
targetType == typeof(decimal))
{
- _stepAttributeValue = "any";
+ return "any";
}
else
{
diff --git a/src/Components/WebAssembly/WebAssembly/src/Rendering/RendererRegistry.cs b/src/Components/WebAssembly/WebAssembly/src/Rendering/RendererRegistry.cs
index 31a063992435..56d4246e23fe 100644
--- a/src/Components/WebAssembly/WebAssembly/src/Rendering/RendererRegistry.cs
+++ b/src/Components/WebAssembly/WebAssembly/src/Rendering/RendererRegistry.cs
@@ -13,16 +13,8 @@ internal static class RendererRegistry
// as well as rooting them for GC purposes, since nothing would otherwise be referencing
// them even though we might still receive incoming events from JS.
+ private static readonly Dictionary? _renderers = OperatingSystem.IsBrowser() ? new() : null;
private static int _nextId;
- private static Dictionary? _renderers;
-
- static RendererRegistry()
- {
- if (OperatingSystem.IsBrowser())
- {
- _renderers = new Dictionary();
- }
- }
internal static WebAssemblyRenderer Find(int rendererId)
{
diff --git a/src/Components/WebAssembly/WebAssembly/src/Rendering/WebAssemblyRenderer.cs b/src/Components/WebAssembly/WebAssembly/src/Rendering/WebAssemblyRenderer.cs
index 672edcbb59bb..b4bad110aedf 100644
--- a/src/Components/WebAssembly/WebAssembly/src/Rendering/WebAssemblyRenderer.cs
+++ b/src/Components/WebAssembly/WebAssembly/src/Rendering/WebAssemblyRenderer.cs
@@ -163,21 +163,16 @@ protected override void HandleException(Exception exception)
private static class Log
{
- private static readonly Action _unhandledExceptionRenderingComponent;
+ private static readonly Action _unhandledExceptionRenderingComponent = LoggerMessage.Define(
+ 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(
- LogLevel.Critical,
- EventIds.UnhandledExceptionRenderingComponent,
- "Unhandled exception rendering component: {Message}");
- }
-
public static void UnhandledExceptionRenderingComponent(ILogger logger, Exception exception)
{
_unhandledExceptionRenderingComponent(
diff --git a/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs b/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs
index 8acd0ceda678..fd4e12403c33 100644
--- a/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs
+++ b/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs
@@ -13,20 +13,13 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Services
internal class WebAssemblyConsoleLogger : ILogger, ILogger
{
private const string _loglevelPadding = ": ";
- private static readonly string _messagePadding;
- private static readonly string _newLineWithMessagePadding;
+ private static readonly string _messagePadding = new(' ', GetLogLevelString(LogLevel.Information).Length + _loglevelPadding.Length);
+ private static readonly string _newLineWithMessagePadding = Environment.NewLine + _messagePadding;
private static readonly StringBuilder _logBuilder = new StringBuilder();
private readonly string _name;
private readonly WebAssemblyJSRuntime _jsRuntime;
- static WebAssemblyConsoleLogger()
- {
- var logLevelString = GetLogLevelString(LogLevel.Information);
- _messagePadding = new string(' ', logLevelString.Length + _loglevelPadding.Length);
- _newLineWithMessagePadding = Environment.NewLine + _messagePadding;
- }
-
public WebAssemblyConsoleLogger(IJSRuntime jsRuntime)
: this(string.Empty, (WebAssemblyJSRuntime)jsRuntime) // Cast for DI
{
diff --git a/src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs b/src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs
index 7dcf39bce16c..b9e89692e2fd 100644
--- a/src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs
+++ b/src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs
@@ -11,28 +11,18 @@
namespace Microsoft.AspNetCore.Hosting
{
- internal static class HostingLoggerExtensions
+ internal static partial class HostingLoggerExtensions
{
- private static readonly Action _startupAssemblyLoaded =
- LoggerMessage.Define(LogLevel.Debug, LoggerEventIds.HostingStartupAssemblyLoaded, "Loaded hosting startup assembly {assemblyName}", skipEnabledCheck: true);
-
- private static readonly Action _listeningOnAddress =
- LoggerMessage.Define(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}")]
+ 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)
{
diff --git a/src/Hosting/Hosting/src/Internal/LoggerEventIds.cs b/src/Hosting/Hosting/src/Internal/LoggerEventIds.cs
index da4bdd9f60c0..916eeae6a2d8 100644
--- a/src/Hosting/Hosting/src/Internal/LoggerEventIds.cs
+++ b/src/Hosting/Hosting/src/Internal/LoggerEventIds.cs
@@ -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;
}
}
diff --git a/src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj b/src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj
index 9babc9ec30e6..38f79972a27c 100644
--- a/src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj
+++ b/src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj
@@ -19,6 +19,7 @@ Microsoft.AspNetCore.Http.HttpResponse
+
diff --git a/src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj b/src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj
index 94a3e73732a3..6eae60ee8936 100644
--- a/src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj
+++ b/src/Http/Http.Extensions/src/Microsoft.AspNetCore.Http.Extensions.csproj
@@ -19,6 +19,7 @@
+
diff --git a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs
index b14ef5fb3d40..5e3d45b6d9ad 100644
--- a/src/Http/Http.Extensions/src/RequestDelegateFactory.cs
+++ b/src/Http/Http.Extensions/src/RequestDelegateFactory.cs
@@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Http
///
/// Creates implementations from request handlers.
///
- 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 _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 _requestBodyInvalidDataException = LoggerMessage.Define(
- LogLevel.Debug,
- new EventId(2, "RequestBodyInvalidDataException"),
- "Reading the request body failed with an InvalidDataException.");
-
- private static readonly Action _parameterBindingFailed = LoggerMessage.Define(
- 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.")]
+ 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.")]
+ 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);
private static ILogger GetLogger(HttpContext httpContext)
{
diff --git a/src/Http/Http/src/Internal/ResponseCookies.cs b/src/Http/Http/src/Internal/ResponseCookies.cs
index c2068821df51..6affe6d9084d 100644
--- a/src/Http/Http/src/Internal/ResponseCookies.cs
+++ b/src/Http/Http/src/Internal/ResponseCookies.cs
@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Http
///
/// A wrapper for the response Set-Cookie header.
///
- 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 _samesiteNotSecure = LoggerMessage.Define(
- 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'.")]
+ public static partial void SameSiteCookieNotSecure(ILogger logger, string name);
}
}
}
diff --git a/src/Http/Routing/src/DefaultLinkGenerator.cs b/src/Http/Routing/src/DefaultLinkGenerator.cs
index b285ac6b5de3..474c6337dc4a 100644
--- a/src/Http/Routing/src/DefaultLinkGenerator.cs
+++ b/src/Http/Routing/src/DefaultLinkGenerator.cs
@@ -18,7 +18,7 @@
namespace Microsoft.AspNetCore.Routing
{
- internal sealed class DefaultLinkGenerator : LinkGenerator, IDisposable
+ internal sealed partial class DefaultLinkGenerator : LinkGenerator, IDisposable
{
private readonly ParameterPolicyFactory _parameterPolicyFactory;
private readonly TemplateBinderFactory _binderFactory;
@@ -336,137 +336,91 @@ public void Dispose()
}
#nullable disable
- private static class Log
+ private static partial class Log
{
- public static class EventIds
- {
- public static readonly EventId EndpointsFound = new EventId(100, "EndpointsFound");
- public static readonly EventId EndpointsNotFound = new EventId(101, "EndpointsNotFound");
-
- public static readonly EventId TemplateSucceeded = new EventId(102, "TemplateSucceeded");
- public static readonly EventId TemplateFailedRequiredValues = new EventId(103, "TemplateFailedRequiredValues");
- public static readonly EventId TemplateFailedConstraint = new EventId(103, "TemplateFailedConstraint");
- public static readonly EventId TemplateFailedExpansion = new EventId(104, "TemplateFailedExpansion");
+ [LoggerMessage(EventId = 100, EventName = "EndpointsFound", Level = LogLevel.Debug, Message = "Found the endpoints {Endpoints} for address {Address}")]
+ public static partial void EndpointsFound(ILogger logger, object address, IEnumerable endpoints);
- public static readonly EventId LinkGenerationSucceeded = new EventId(105, "LinkGenerationSucceeded");
- public static readonly EventId LinkGenerationFailed = new EventId(106, "LinkGenerationFailed");
- }
-
- private static readonly Action, object, Exception> _endpointsFound = LoggerMessage.Define, object>(
- LogLevel.Debug,
- EventIds.EndpointsFound,
- "Found the endpoints {Endpoints} for address {Address}",
- skipEnabledCheck: true);
-
- private static readonly Action _endpointsNotFound = LoggerMessage.Define