Skip to content

Commit 2857014

Browse files
Move logging to separate class to prevent allocation per generic type (#31345)
1 parent c1c4c58 commit 2857014

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Microsoft.AspNetCore.Server.HttpSys
8+
{
9+
internal static class RequestContextLog
10+
{
11+
private static readonly Action<ILogger, Exception?> _requestError =
12+
LoggerMessage.Define(LogLevel.Error, LoggerEventIds.RequestError, "ProcessRequestAsync");
13+
14+
private static readonly Action<ILogger, Exception?> _requestProcessError =
15+
LoggerMessage.Define(LogLevel.Error, LoggerEventIds.RequestProcessError, "ProcessRequestAsync");
16+
17+
private static readonly Action<ILogger, Exception?> _requestsDrained =
18+
LoggerMessage.Define(LogLevel.Information, LoggerEventIds.RequestsDrained, "All requests drained.");
19+
20+
public static void RequestError(ILogger logger, Exception exception)
21+
{
22+
_requestError(logger, exception);
23+
}
24+
25+
public static void RequestProcessError(ILogger logger, Exception exception)
26+
{
27+
_requestProcessError(logger, exception);
28+
}
29+
30+
public static void RequestsDrained(ILogger logger)
31+
{
32+
_requestsDrained(logger, null);
33+
}
34+
}
35+
}

src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.Log.cs

-38
This file was deleted.

src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected override async Task ExecuteAsync()
5151
}
5252
catch (Exception ex)
5353
{
54-
Log.RequestProcessError(Logger, ex);
54+
RequestContextLog.RequestProcessError(Logger, ex);
5555
if (context != null)
5656
{
5757
application.DisposeContext(context, ex);
@@ -85,14 +85,14 @@ protected override async Task ExecuteAsync()
8585
{
8686
if (messagePump.DecrementOutstandingRequest() == 0 && messagePump.Stopping)
8787
{
88-
Log.RequestsDrained(Logger);
88+
RequestContextLog.RequestsDrained(Logger);
8989
messagePump.SetShutdownSignal();
9090
}
9191
}
9292
}
9393
catch (Exception ex)
9494
{
95-
Log.RequestError(Logger, ex);
95+
RequestContextLog.RequestError(Logger, ex);
9696
Abort();
9797
}
9898
}

0 commit comments

Comments
 (0)