Skip to content

Commit 3a214f2

Browse files
authored
Avoid using HttpRequest from log scope (#9979) (#9980) (#9981)
* Avoid using HttpRequest from log scope (#9979) (#9980) * Increase patch version
1 parent 4bf9795 commit 3a214f2

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

build/common.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<LangVersion>latest</LangVersion>
66
<MajorVersion>4</MajorVersion>
77
<MinorVersion>$(MinorVersionPrefix)33</MinorVersion>
8-
<PatchVersion>0</PatchVersion>
8+
<PatchVersion>1</PatchVersion>
99
<BuildNumber Condition="'$(BuildNumber)' == '' ">0</BuildNumber>
1010
<PreviewVersion></PreviewVersion>
1111

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
@@ -133,6 +133,7 @@ private async Task<IActionResult> GetResultAsync(HttpContext context, IFunctionE
133133
var scopeState = new Dictionary<string, object>()
134134
{
135135
[ScriptConstants.LoggerHttpRequest] = context.Request,
136+
[ScriptConstants.AzureFunctionsRequestIdKey] = context.Request.GetRequestId(),
136137
};
137138

138139
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

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

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

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

0 commit comments

Comments
 (0)