From 45e103d50bf245c2ae6cfb5a0b3ff81300462246 Mon Sep 17 00:00:00 2001 From: andrzej Date: Mon, 29 Mar 2021 09:03:33 +0200 Subject: [PATCH] Move logging to separate class to prevent allocation per generic type --- .../RequestProcessing/RequestContextLog.cs | 35 +++++++++++++++++ .../RequestContextOfT.Log.cs | 38 ------------------- .../RequestProcessing/RequestContextOfT.cs | 6 +-- 3 files changed, 38 insertions(+), 41 deletions(-) create mode 100644 src/Servers/HttpSys/src/RequestProcessing/RequestContextLog.cs delete mode 100644 src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.Log.cs diff --git a/src/Servers/HttpSys/src/RequestProcessing/RequestContextLog.cs b/src/Servers/HttpSys/src/RequestProcessing/RequestContextLog.cs new file mode 100644 index 000000000000..c1eff6ad8df8 --- /dev/null +++ b/src/Servers/HttpSys/src/RequestProcessing/RequestContextLog.cs @@ -0,0 +1,35 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.Extensions.Logging; + +namespace Microsoft.AspNetCore.Server.HttpSys +{ + internal static class RequestContextLog + { + private static readonly Action _requestError = + LoggerMessage.Define(LogLevel.Error, LoggerEventIds.RequestError, "ProcessRequestAsync"); + + private static readonly Action _requestProcessError = + LoggerMessage.Define(LogLevel.Error, LoggerEventIds.RequestProcessError, "ProcessRequestAsync"); + + private static readonly Action _requestsDrained = + LoggerMessage.Define(LogLevel.Information, LoggerEventIds.RequestsDrained, "All requests drained."); + + public static void RequestError(ILogger logger, Exception exception) + { + _requestError(logger, exception); + } + + public static void RequestProcessError(ILogger logger, Exception exception) + { + _requestProcessError(logger, exception); + } + + public static void RequestsDrained(ILogger logger) + { + _requestsDrained(logger, null); + } + } +} diff --git a/src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.Log.cs b/src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.Log.cs deleted file mode 100644 index f387bf179db4..000000000000 --- a/src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.Log.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.Extensions.Logging; - -namespace Microsoft.AspNetCore.Server.HttpSys -{ - internal sealed partial class RequestContext - { - private static class Log - { - private static readonly Action _requestError = - LoggerMessage.Define(LogLevel.Error, LoggerEventIds.RequestError, "ProcessRequestAsync"); - - private static readonly Action _requestProcessError = - LoggerMessage.Define(LogLevel.Error, LoggerEventIds.RequestProcessError, "ProcessRequestAsync"); - - private static readonly Action _requestsDrained = - LoggerMessage.Define(LogLevel.Information, LoggerEventIds.RequestsDrained, "All requests drained."); - - public static void RequestError(ILogger logger, Exception exception) - { - _requestError(logger, exception); - } - - public static void RequestProcessError(ILogger logger, Exception exception) - { - _requestProcessError(logger, exception); - } - - public static void RequestsDrained(ILogger logger) - { - _requestsDrained(logger, null); - } - } - } -} diff --git a/src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.cs b/src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.cs index 1d7eb0412e02..88947e041f8c 100644 --- a/src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.cs +++ b/src/Servers/HttpSys/src/RequestProcessing/RequestContextOfT.cs @@ -51,7 +51,7 @@ protected override async Task ExecuteAsync() } catch (Exception ex) { - Log.RequestProcessError(Logger, ex); + RequestContextLog.RequestProcessError(Logger, ex); if (context != null) { application.DisposeContext(context, ex); @@ -85,14 +85,14 @@ protected override async Task ExecuteAsync() { if (messagePump.DecrementOutstandingRequest() == 0 && messagePump.Stopping) { - Log.RequestsDrained(Logger); + RequestContextLog.RequestsDrained(Logger); messagePump.SetShutdownSignal(); } } } catch (Exception ex) { - Log.RequestError(Logger, ex); + RequestContextLog.RequestError(Logger, ex); Abort(); } }