Skip to content

Commit b700636

Browse files
authored
Use skipEnabledCheck for LoggerMessage.Define (#31562)
* 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
1 parent 7b04ecf commit b700636

File tree

32 files changed

+415
-320
lines changed

32 files changed

+415
-320
lines changed

src/Components/Components/src/RenderTree/Renderer.Log.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ public abstract partial class Renderer
1414
internal static class Log
1515
{
1616
private static readonly Action<ILogger, int, Type, int, Type, Exception> _initializingChildComponent =
17-
LoggerMessage.Define<int, Type, int, Type>(LogLevel.Debug, new EventId(1, "InitializingChildComponent"), "Initializing component {ComponentId} ({ComponentType}) as child of {ParentComponentId} ({ParentComponentId})");
17+
LoggerMessage.Define<int, Type, int, Type>(LogLevel.Debug, new EventId(1, "InitializingChildComponent"), "Initializing component {ComponentId} ({ComponentType}) as child of {ParentComponentId} ({ParentComponentId})", skipEnabledCheck: true);
1818

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

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

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

2828
private static readonly Action<ILogger, ulong, string, Exception> _handlingEvent =
2929
LoggerMessage.Define<ulong, string>(LogLevel.Debug, new EventId(5, "HandlingEvent"), "Handling event {EventId} of type '{EventType}'");

src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Hosting
1414
internal static class HostingLoggerExtensions
1515
{
1616
private static readonly Action<ILogger, string, Exception?> _startupAssemblyLoaded =
17-
LoggerMessage.Define<string>(LogLevel.Debug, LoggerEventIds.HostingStartupAssemblyLoaded, "Loaded hosting startup assembly {assemblyName}");
17+
LoggerMessage.Define<string>(LogLevel.Debug, LoggerEventIds.HostingStartupAssemblyLoaded, "Loaded hosting startup assembly {assemblyName}", skipEnabledCheck: true);
1818

1919
private static readonly Action<ILogger, string, Exception?> _listeningOnAddress =
2020
LoggerMessage.Define<string>(LogLevel.Information, LoggerEventIds.ServerListeningOnAddresses, "Now listening on: {address}");

src/Http/Routing/src/DefaultLinkGenerator.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ public static class EventIds
355355
private static readonly Action<ILogger, IEnumerable<string>, object, Exception> _endpointsFound = LoggerMessage.Define<IEnumerable<string>, object>(
356356
LogLevel.Debug,
357357
EventIds.EndpointsFound,
358-
"Found the endpoints {Endpoints} for address {Address}");
358+
"Found the endpoints {Endpoints} for address {Address}",
359+
skipEnabledCheck: true);
359360

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

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

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

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

395400
private static readonly Action<ILogger, IEnumerable<string>, Exception> _linkGenerationFailed = LoggerMessage.Define<IEnumerable<string>>(
396401
LogLevel.Debug,
397402
EventIds.LinkGenerationFailed,
398-
"Link generation failed for endpoints {Endpoints}");
403+
"Link generation failed for endpoints {Endpoints}",
404+
skipEnabledCheck: true);
399405

400406
public static void EndpointsFound(ILogger logger, object address, IEnumerable<Endpoint> endpoints)
401407
{

src/Http/Routing/src/DefaultLinkParser.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ public static class EventIds
184184
private static readonly Action<ILogger, IEnumerable<string>, object, Exception> _endpointsFound = LoggerMessage.Define<IEnumerable<string>, object>(
185185
LogLevel.Debug,
186186
EventIds.EndpointsFound,
187-
"Found the endpoints {Endpoints} for address {Address}");
187+
"Found the endpoints {Endpoints} for address {Address}",
188+
skipEnabledCheck: true);
188189

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

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

204207
public static void EndpointsFound(ILogger logger, object address, IEnumerable<Endpoint> endpoints)
205208
{

src/Http/Routing/src/Matching/DfaMatcher.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,32 +339,38 @@ private static class Logger
339339
private static readonly Action<ILogger, string, Exception> _candidatesNotFound = LoggerMessage.Define<string>(
340340
LogLevel.Debug,
341341
EventIds.CandidatesNotFound,
342-
"No candidates found for the request path '{Path}'");
342+
"No candidates found for the request path '{Path}'",
343+
skipEnabledCheck: true);
343344

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

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

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

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

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

369375
public static void CandidatesNotFound(ILogger logger, string path)
370376
{

src/Middleware/HostFiltering/src/LoggerExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static class LoggerExtensions
1212
LoggerMessage.Define(LogLevel.Debug, new EventId(0, "WildcardDetected"), "Wildcard detected, all requests with hosts will be allowed.");
1313

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

1717
private static readonly Action<ILogger, Exception?> _allHostsAllowed =
1818
LoggerMessage.Define(LogLevel.Trace, new EventId(2, "AllHostsAllowed"), "All hosts are allowed.");

src/Middleware/Session/src/LoggingExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,22 @@ static LoggingExtensions()
3434
_sessionStarted = LoggerMessage.Define<string, string>(
3535
eventId: new EventId(3, "SessionStarted"),
3636
logLevel: LogLevel.Information,
37-
formatString: "Session started; Key:{sessionKey}, Id:{sessionId}");
37+
formatString: "Session started; Key:{sessionKey}, Id:{sessionId}",
38+
skipEnabledCheck: true);
3839
_sessionLoaded = LoggerMessage.Define<string, string, int>(
3940
eventId: new EventId(4, "SessionLoaded"),
4041
logLevel: LogLevel.Debug,
41-
formatString: "Session loaded; Key:{sessionKey}, Id:{sessionId}, Count:{count}");
42+
formatString: "Session loaded; Key:{sessionKey}, Id:{sessionId}, Count:{count}",
43+
skipEnabledCheck: true);
4244
_sessionStored = LoggerMessage.Define<string, string, int>(
4345
eventId: new EventId(5, "SessionStored"),
4446
logLevel: LogLevel.Debug,
4547
formatString: "Session stored; Key:{sessionKey}, Id:{sessionId}, Count:{count}");
4648
_sessionCacheReadException = LoggerMessage.Define<string>(
4749
eventId: new EventId(6, "SessionCacheReadException"),
4850
logLevel: LogLevel.Error,
49-
formatString: "Session cache read exception, Key:{sessionKey}");
51+
formatString: "Session cache read exception, Key:{sessionKey}",
52+
skipEnabledCheck: true);
5053
_errorUnprotectingCookie = LoggerMessage.Define(
5154
eventId: new EventId(7, "ErrorUnprotectingCookie"),
5255
logLevel: LogLevel.Warning,

src/Mvc/Mvc.Core/src/Infrastructure/ObjectResultExecutor.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,11 @@ private static void InferContentTypes(ActionContext context, ObjectResult result
168168

169169
private static class Log
170170
{
171-
private static readonly Action<ILogger, string?, Exception?> _bufferingAsyncEnumerable;
172-
173-
static Log()
174-
{
175-
_bufferingAsyncEnumerable = LoggerMessage.Define<string?>(
176-
LogLevel.Debug,
177-
new EventId(1, "BufferingAsyncEnumerable"),
178-
"Buffering IAsyncEnumerable instance of type '{Type}'.");
179-
}
171+
private static readonly Action<ILogger, string?, Exception?> _bufferingAsyncEnumerable = LoggerMessage.Define<string?>(
172+
LogLevel.Debug,
173+
new EventId(1, "BufferingAsyncEnumerable"),
174+
"Buffering IAsyncEnumerable instance of type '{Type}'.",
175+
skipEnabledCheck: true);
180176

181177
public static void BufferingAsyncEnumerable(ILogger logger, object asyncEnumerable)
182178
{

src/Mvc/Mvc.Core/src/Infrastructure/SystemTextJsonResultExecutor.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ private static class Log
144144
private static readonly Action<ILogger, string?, Exception?> _jsonResultExecuting = LoggerMessage.Define<string?>(
145145
LogLevel.Information,
146146
new EventId(1, "JsonResultExecuting"),
147-
"Executing JsonResult, writing value of type '{Type}'.");
147+
"Executing JsonResult, writing value of type '{Type}'.",
148+
skipEnabledCheck: true);
148149

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

154156
public static void JsonResultExecuting(ILogger logger, object? value)
155157
{

0 commit comments

Comments
 (0)