Skip to content

Commit

Permalink
Use skipEnabledCheck for LoggerMessage.Define (#31562)
Browse files Browse the repository at this point in the history
* Use skipEnabledCheck in MVC

* Use skipEnabledCheck in Http/Routing DefaultLinkGenerator, DefaultLinkParser, and DfaMatcher

* Use skipEnabledCheck in SignalR

* Use skipEnabledCheck in Components

* Use skipEnabledCheck in Hosting

* Use skipEnabledCheck in Servers/HttpSys

* Use skipEnabledCheck in Servers/Kestrel

* Use skipEnabledCheck in Middleware

* Avoid some allocations by checked IsEnabled first in MVC

* Avoid some allocations by checked IsEnabled first in Servers/Kestrel

* Simplify usage of logging in transports
  • Loading branch information
gfoidl authored Apr 7, 2021
1 parent 7b04ecf commit b700636
Show file tree
Hide file tree
Showing 32 changed files with 415 additions and 320 deletions.
8 changes: 4 additions & 4 deletions src/Components/Components/src/RenderTree/Renderer.Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public abstract partial class Renderer
internal static class Log
{
private static readonly Action<ILogger, int, Type, int, Type, Exception> _initializingChildComponent =
LoggerMessage.Define<int, Type, int, Type>(LogLevel.Debug, new EventId(1, "InitializingChildComponent"), "Initializing component {ComponentId} ({ComponentType}) as child of {ParentComponentId} ({ParentComponentId})");
LoggerMessage.Define<int, Type, int, Type>(LogLevel.Debug, new EventId(1, "InitializingChildComponent"), "Initializing component {ComponentId} ({ComponentType}) as child of {ParentComponentId} ({ParentComponentId})", skipEnabledCheck: true);

private static readonly Action<ILogger, int, Type, Exception> _initializingRootComponent =
LoggerMessage.Define<int, Type>(LogLevel.Debug, new EventId(2, "InitializingRootComponent"), "Initializing root component {ComponentId} ({ComponentType})");
LoggerMessage.Define<int, Type>(LogLevel.Debug, new EventId(2, "InitializingRootComponent"), "Initializing root component {ComponentId} ({ComponentType})", skipEnabledCheck: true);

private static readonly Action<ILogger, int, Type, Exception> _renderingComponent =
LoggerMessage.Define<int, Type>(LogLevel.Debug, new EventId(3, "RenderingComponent"), "Rendering component {ComponentId} of type {ComponentType}");
LoggerMessage.Define<int, Type>(LogLevel.Debug, new EventId(3, "RenderingComponent"), "Rendering component {ComponentId} of type {ComponentType}", skipEnabledCheck: true);

private static readonly Action<ILogger, int, Type, Exception> _disposingComponent =
LoggerMessage.Define<int, Type>(LogLevel.Debug, new EventId(4, "DisposingComponent"), "Disposing component {ComponentId} of type {ComponentType}");
LoggerMessage.Define<int, Type>(LogLevel.Debug, new EventId(4, "DisposingComponent"), "Disposing component {ComponentId} of type {ComponentType}", skipEnabledCheck: true);

private static readonly Action<ILogger, ulong, string, Exception> _handlingEvent =
LoggerMessage.Define<ulong, string>(LogLevel.Debug, new EventId(5, "HandlingEvent"), "Handling event {EventId} of type '{EventType}'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Hosting
internal static class HostingLoggerExtensions
{
private static readonly Action<ILogger, string, Exception?> _startupAssemblyLoaded =
LoggerMessage.Define<string>(LogLevel.Debug, LoggerEventIds.HostingStartupAssemblyLoaded, "Loaded hosting startup assembly {assemblyName}");
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}");
Expand Down
18 changes: 12 additions & 6 deletions src/Http/Routing/src/DefaultLinkGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ public static class EventIds
private static readonly Action<ILogger, IEnumerable<string>, object, Exception> _endpointsFound = LoggerMessage.Define<IEnumerable<string>, object>(
LogLevel.Debug,
EventIds.EndpointsFound,
"Found the endpoints {Endpoints} for address {Address}");
"Found the endpoints {Endpoints} for address {Address}",
skipEnabledCheck: true);

private static readonly Action<ILogger, object, Exception> _endpointsNotFound = LoggerMessage.Define<object>(
LogLevel.Debug,
Expand All @@ -372,30 +373,35 @@ public static class EventIds
EventIds.TemplateFailedRequiredValues,
"Failed to process the template {Template} for {Endpoint}. " +
"A required route value is missing, or has a different value from the required default values. " +
"Supplied ambient values {AmbientValues} and {Values} with default values {Defaults}");
"Supplied ambient values {AmbientValues} and {Values} with default values {Defaults}",
skipEnabledCheck: true);

private static readonly Action<ILogger, string, string, IRouteConstraint, string, string, Exception> _templateFailedConstraint = LoggerMessage.Define<string, string, IRouteConstraint, string, string>(
LogLevel.Debug,
EventIds.TemplateFailedConstraint,
"Failed to process the template {Template} for {Endpoint}. " +
"The constraint {Constraint} for parameter {ParameterName} failed with values {Values}");
"The constraint {Constraint} for parameter {ParameterName} failed with values {Values}",
skipEnabledCheck: true);

private static readonly Action<ILogger, string, string, string, Exception> _templateFailedExpansion = LoggerMessage.Define<string, string, string>(
LogLevel.Debug,
EventIds.TemplateFailedExpansion,
"Failed to process the template {Template} for {Endpoint}. " +
"The failure occurred while expanding the template with values {Values} " +
"This is usually due to a missing or empty value in a complex segment");
"This is usually due to a missing or empty value in a complex segment",
skipEnabledCheck: true);

private static readonly Action<ILogger, IEnumerable<string>, string, Exception> _linkGenerationSucceeded = LoggerMessage.Define<IEnumerable<string>, string>(
LogLevel.Debug,
EventIds.LinkGenerationSucceeded,
"Link generation succeeded for endpoints {Endpoints} with result {URI}");
"Link generation succeeded for endpoints {Endpoints} with result {URI}",
skipEnabledCheck: true);

private static readonly Action<ILogger, IEnumerable<string>, Exception> _linkGenerationFailed = LoggerMessage.Define<IEnumerable<string>>(
LogLevel.Debug,
EventIds.LinkGenerationFailed,
"Link generation failed for endpoints {Endpoints}");
"Link generation failed for endpoints {Endpoints}",
skipEnabledCheck: true);

public static void EndpointsFound(ILogger logger, object address, IEnumerable<Endpoint> endpoints)
{
Expand Down
9 changes: 6 additions & 3 deletions src/Http/Routing/src/DefaultLinkParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public static class EventIds
private static readonly Action<ILogger, IEnumerable<string>, object, Exception> _endpointsFound = LoggerMessage.Define<IEnumerable<string>, object>(
LogLevel.Debug,
EventIds.EndpointsFound,
"Found the endpoints {Endpoints} for address {Address}");
"Found the endpoints {Endpoints} for address {Address}",
skipEnabledCheck: true);

private static readonly Action<ILogger, object, Exception> _endpointsNotFound = LoggerMessage.Define<object>(
LogLevel.Debug,
Expand All @@ -194,12 +195,14 @@ public static class EventIds
private static readonly Action<ILogger, string, string, Exception> _pathParsingSucceeded = LoggerMessage.Define<string, string>(
LogLevel.Debug,
EventIds.PathParsingSucceeded,
"Path parsing succeeded for endpoint {Endpoint} and URI path {URI}");
"Path parsing succeeded for endpoint {Endpoint} and URI path {URI}",
skipEnabledCheck: true);

private static readonly Action<ILogger, IEnumerable<string>, string, Exception> _pathParsingFailed = LoggerMessage.Define<IEnumerable<string>, string>(
LogLevel.Debug,
EventIds.PathParsingFailed,
"Path parsing failed for endpoints {Endpoints} and URI path {URI}");
"Path parsing failed for endpoints {Endpoints} and URI path {URI}",
skipEnabledCheck: true);

public static void EndpointsFound(ILogger logger, object address, IEnumerable<Endpoint> endpoints)
{
Expand Down
18 changes: 12 additions & 6 deletions src/Http/Routing/src/Matching/DfaMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,32 +339,38 @@ private static class Logger
private static readonly Action<ILogger, string, Exception> _candidatesNotFound = LoggerMessage.Define<string>(
LogLevel.Debug,
EventIds.CandidatesNotFound,
"No candidates found for the request path '{Path}'");
"No candidates found for the request path '{Path}'",
skipEnabledCheck: true);

private static readonly Action<ILogger, int, string, Exception> _candidatesFound = LoggerMessage.Define<int, string>(
LogLevel.Debug,
EventIds.CandidatesFound,
"{CandidateCount} candidate(s) found for the request path '{Path}'");
"{CandidateCount} candidate(s) found for the request path '{Path}'",
skipEnabledCheck: true);

private static readonly Action<ILogger, string, string, string, string, Exception> _candidateRejectedByComplexSegment = LoggerMessage.Define<string, string, string, string>(
LogLevel.Debug,
EventIds.CandidateRejectedByComplexSegment,
"Endpoint '{Endpoint}' with route pattern '{RoutePattern}' was rejected by complex segment '{Segment}' for the request path '{Path}'");
"Endpoint '{Endpoint}' with route pattern '{RoutePattern}' was rejected by complex segment '{Segment}' for the request path '{Path}'",
skipEnabledCheck: true);

private static readonly Action<ILogger, string, string, string, string, object, string, Exception> _candidateRejectedByConstraint = LoggerMessage.Define<string, string, string, string, object, string>(
LogLevel.Debug,
EventIds.CandidateRejectedByConstraint,
"Endpoint '{Endpoint}' with route pattern '{RoutePattern}' was rejected by constraint '{ConstraintName}':'{Constraint}' with value '{RouteValue}' for the request path '{Path}'");
"Endpoint '{Endpoint}' with route pattern '{RoutePattern}' was rejected by constraint '{ConstraintName}':'{Constraint}' with value '{RouteValue}' for the request path '{Path}'",
skipEnabledCheck: true);

private static readonly Action<ILogger, string, string, string, Exception> _candidateNotValid = LoggerMessage.Define<string, string, string>(
LogLevel.Debug,
EventIds.CandidateNotValid,
"Endpoint '{Endpoint}' with route pattern '{RoutePattern}' is not valid for the request path '{Path}'");
"Endpoint '{Endpoint}' with route pattern '{RoutePattern}' is not valid for the request path '{Path}'",
skipEnabledCheck: true);

private static readonly Action<ILogger, string, string, string, Exception> _candidateValid = LoggerMessage.Define<string, string, string>(
LogLevel.Debug,
EventIds.CandidateValid,
"Endpoint '{Endpoint}' with route pattern '{RoutePattern}' is valid for the request path '{Path}'");
"Endpoint '{Endpoint}' with route pattern '{RoutePattern}' is valid for the request path '{Path}'",
skipEnabledCheck: true);

public static void CandidatesNotFound(ILogger logger, string path)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/HostFiltering/src/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class LoggerExtensions
LoggerMessage.Define(LogLevel.Debug, new EventId(0, "WildcardDetected"), "Wildcard detected, all requests with hosts will be allowed.");

private static readonly Action<ILogger, string, Exception?> _allowedHosts =
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "AllowedHosts"), "Allowed hosts: {Hosts}");
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "AllowedHosts"), "Allowed hosts: {Hosts}", skipEnabledCheck: true);

private static readonly Action<ILogger, Exception?> _allHostsAllowed =
LoggerMessage.Define(LogLevel.Trace, new EventId(2, "AllHostsAllowed"), "All hosts are allowed.");
Expand Down
9 changes: 6 additions & 3 deletions src/Middleware/Session/src/LoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ static LoggingExtensions()
_sessionStarted = LoggerMessage.Define<string, string>(
eventId: new EventId(3, "SessionStarted"),
logLevel: LogLevel.Information,
formatString: "Session started; Key:{sessionKey}, Id:{sessionId}");
formatString: "Session started; Key:{sessionKey}, Id:{sessionId}",
skipEnabledCheck: true);
_sessionLoaded = LoggerMessage.Define<string, string, int>(
eventId: new EventId(4, "SessionLoaded"),
logLevel: LogLevel.Debug,
formatString: "Session loaded; Key:{sessionKey}, Id:{sessionId}, Count:{count}");
formatString: "Session loaded; Key:{sessionKey}, Id:{sessionId}, Count:{count}",
skipEnabledCheck: true);
_sessionStored = LoggerMessage.Define<string, string, int>(
eventId: new EventId(5, "SessionStored"),
logLevel: LogLevel.Debug,
formatString: "Session stored; Key:{sessionKey}, Id:{sessionId}, Count:{count}");
_sessionCacheReadException = LoggerMessage.Define<string>(
eventId: new EventId(6, "SessionCacheReadException"),
logLevel: LogLevel.Error,
formatString: "Session cache read exception, Key:{sessionKey}");
formatString: "Session cache read exception, Key:{sessionKey}",
skipEnabledCheck: true);
_errorUnprotectingCookie = LoggerMessage.Define(
eventId: new EventId(7, "ErrorUnprotectingCookie"),
logLevel: LogLevel.Warning,
Expand Down
14 changes: 5 additions & 9 deletions src/Mvc/Mvc.Core/src/Infrastructure/ObjectResultExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,11 @@ private static void InferContentTypes(ActionContext context, ObjectResult result

private static class Log
{
private static readonly Action<ILogger, string?, Exception?> _bufferingAsyncEnumerable;

static Log()
{
_bufferingAsyncEnumerable = LoggerMessage.Define<string?>(
LogLevel.Debug,
new EventId(1, "BufferingAsyncEnumerable"),
"Buffering IAsyncEnumerable instance of type '{Type}'.");
}
private static readonly Action<ILogger, string?, Exception?> _bufferingAsyncEnumerable = LoggerMessage.Define<string?>(
LogLevel.Debug,
new EventId(1, "BufferingAsyncEnumerable"),
"Buffering IAsyncEnumerable instance of type '{Type}'.",
skipEnabledCheck: true);

public static void BufferingAsyncEnumerable(ILogger logger, object asyncEnumerable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ private static class Log
private static readonly Action<ILogger, string?, Exception?> _jsonResultExecuting = LoggerMessage.Define<string?>(
LogLevel.Information,
new EventId(1, "JsonResultExecuting"),
"Executing JsonResult, writing value of type '{Type}'.");
"Executing JsonResult, writing value of type '{Type}'.",
skipEnabledCheck: true);

private static readonly Action<ILogger, string?, Exception?> _bufferingAsyncEnumerable = LoggerMessage.Define<string?>(
LogLevel.Debug,
new EventId(2, "BufferingAsyncEnumerable"),
"Buffering IAsyncEnumerable instance of type '{Type}'.");
"Buffering IAsyncEnumerable instance of type '{Type}'.",
skipEnabledCheck: true);

public static void JsonResultExecuting(ILogger logger, object? value)
{
Expand Down
Loading

0 comments on commit b700636

Please sign in to comment.