Eliminate TraceIdentifer allocation when there is a request bo… (#19325)

This commit is contained in:
James Newton-King 2020-02-26 09:53:34 +13:00 committed by GitHub
parent 9f0458739c
commit ff59c45505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -6,6 +6,7 @@ using System.IO.Pipelines;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
{ {
@ -93,7 +94,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
if (!RequestUpgrade) if (!RequestUpgrade)
{ {
Log.RequestBodyStart(_context.ConnectionIdFeature, _context.TraceIdentifier); // Accessing TraceIdentifier will lazy-allocate a string ID.
// Don't access TraceIdentifer unless logging is enabled.
if (Log.IsEnabled(LogLevel.Debug))
{
Log.RequestBodyStart(_context.ConnectionIdFeature, _context.TraceIdentifier);
}
if (_context.MinRequestBodyDataRate != null) if (_context.MinRequestBodyDataRate != null)
{ {
@ -116,7 +122,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
if (!RequestUpgrade) if (!RequestUpgrade)
{ {
Log.RequestBodyDone(_context.ConnectionIdFeature, _context.TraceIdentifier); // Accessing TraceIdentifier will lazy-allocate a string ID
// Don't access TraceIdentifer unless logging is enabled.
if (Log.IsEnabled(LogLevel.Debug))
{
Log.RequestBodyDone(_context.ConnectionIdFeature, _context.TraceIdentifier);
}
if (_timingEnabled) if (_timingEnabled)
{ {

View File

@ -810,6 +810,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
using (var input = new TestInput()) using (var input = new TestInput())
{ {
var mockLogger = new Mock<IKestrelTrace>(); var mockLogger = new Mock<IKestrelTrace>();
mockLogger
.Setup(logger => logger.IsEnabled(Extensions.Logging.LogLevel.Debug))
.Returns(true);
input.Http1Connection.ServiceContext.Log = mockLogger.Object; input.Http1Connection.ServiceContext.Log = mockLogger.Object;
input.Http1Connection.ConnectionIdFeature = "ConnectionId"; input.Http1Connection.ConnectionIdFeature = "ConnectionId";
input.Http1Connection.TraceIdentifier = "RequestId"; input.Http1Connection.TraceIdentifier = "RequestId";
@ -841,6 +844,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests
mockLogger mockLogger
.Setup(logger => logger.RequestBodyDone("ConnectionId", "RequestId")) .Setup(logger => logger.RequestBodyDone("ConnectionId", "RequestId"))
.Callback(() => logEvent.SetResult(null)); .Callback(() => logEvent.SetResult(null));
mockLogger
.Setup(logger => logger.IsEnabled(Extensions.Logging.LogLevel.Debug))
.Returns(true);
input.Http1Connection.ServiceContext.Log = mockLogger.Object; input.Http1Connection.ServiceContext.Log = mockLogger.Object;
input.Http1Connection.ConnectionIdFeature = "ConnectionId"; input.Http1Connection.ConnectionIdFeature = "ConnectionId";
input.Http1Connection.TraceIdentifier = "RequestId"; input.Http1Connection.TraceIdentifier = "RequestId";