Logging optimization.2 14 (#944)

* Trivial change to avoid an allocation when logging is off (normal case).
This commit is contained in:
Vance Morrison 2017-03-28 11:43:58 -07:00 committed by David Fowler
parent f79904404e
commit 8165609f4e
2 changed files with 6 additions and 2 deletions

View File

@ -75,7 +75,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
return new Context
{
HttpContext = httpContext,
Scope = scope,
Scope = scope, // Scope can be null if logging is not on.
StartTimestamp = startTimestamp,
};
}

View File

@ -15,7 +15,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal
{
public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext)
{
return logger.BeginScope(new HostingLogScope(httpContext));
// to avoid allocation, return a null scope if the logger is not on at least to some degree.
if (logger.IsEnabled(LogLevel.Critical))
return logger.BeginScope(new HostingLogScope(httpContext));
else
return null;
}
public static void ApplicationError(this ILogger logger, Exception exception)