Skip to content

Commit c7c042f

Browse files
authored
Avoid using HttpRequest from log scope (#9979) (#9980)
1 parent 5a27862 commit c7c042f

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/WebJobs.Script.WebHost/Diagnostics/SystemLogger.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,9 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
146146

147147
// For Http function invocations we want to stamp invocation logs with
148148
// the request ID for easy correlation with incoming Http request logs.
149-
if (scopeProps.TryGetValue(ScriptConstants.LoggerHttpRequest, out scopeValue))
149+
if (scopeProps.TryGetValue(ScriptConstants.AzureFunctionsRequestIdKey, out scopeValue))
150150
{
151-
var httpRequest = (HttpRequest)scopeValue;
152-
scopeActivityId = httpRequest.GetRequestId();
151+
scopeActivityId = scopeValue as string;
153152
}
154153
}
155154

src/WebJobs.Script.WebHost/Middleware/FunctionInvocationMiddleware.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ private async Task<IActionResult> GetResultAsync(HttpContext context, IFunctionE
136136
var scopeState = new Dictionary<string, object>()
137137
{
138138
[ScriptConstants.LoggerHttpRequest] = context.Request,
139+
[ScriptConstants.AzureFunctionsRequestIdKey] = context.Request.GetRequestId(),
139140
};
140141

141142
using (logger.BeginScope(scopeState))

src/WebJobs.Script/ScriptConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Immutable;
56
using NuGet.Versioning;
67

@@ -46,6 +47,10 @@ public static class ScriptConstants
4647
public const string TraceSourceHttpHandler = "HttpRequestTraceHandler";
4748
public const string TraceSourceHttpThrottleMiddleware = "HttpThrottleMiddleware";
4849

50+
// We have been using this to insert the full HttpRequest into the logger scope. This is not a good practice
51+
// as it will worsen any execution context leaks. If logger scope is leaked, now HTTP request will be leaked as well.
52+
// We will not remove it from the logger scope just yet, but we will remove all of our own usage of it.
53+
[Obsolete("Do not access the HTTP request from the logger scope, this will be removed in a future version.")]
4954
public const string LoggerHttpRequest = "MS_HttpRequest";
5055

5156
public const string LogCategoryHostController = "Host.Controllers.Host";

0 commit comments

Comments
 (0)