Skip to content

Commit 8165609

Browse files
vancemdavidfowl
authored andcommitted
Logging optimization.2 14 (#944)
* Trivial change to avoid an allocation when logging is off (normal case).
1 parent f799044 commit 8165609

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/Microsoft.AspNetCore.Hosting/Internal/HostingApplication.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Context CreateContext(IFeatureCollection contextFeatures)
7575
return new Context
7676
{
7777
HttpContext = httpContext,
78-
Scope = scope,
78+
Scope = scope, // Scope can be null if logging is not on.
7979
StartTimestamp = startTimestamp,
8080
};
8181
}

src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ internal static class HostingLoggerExtensions
1515
{
1616
public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext)
1717
{
18-
return logger.BeginScope(new HostingLogScope(httpContext));
18+
// to avoid allocation, return a null scope if the logger is not on at least to some degree.
19+
if (logger.IsEnabled(LogLevel.Critical))
20+
return logger.BeginScope(new HostingLogScope(httpContext));
21+
else
22+
return null;
1923
}
2024

2125
public static void ApplicationError(this ILogger logger, Exception exception)

0 commit comments

Comments
 (0)