Skip to content

Commit

Permalink
Avoid using HttpRequest from log scope (#9979) (#9980) (#9981)
Browse files Browse the repository at this point in the history
* Avoid using HttpRequest from log scope (#9979) (#9980)

* Increase patch version
  • Loading branch information
jviau authored Apr 5, 2024
1 parent 4bf9795 commit 3a214f2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion>
<MajorVersion>4</MajorVersion>
<MinorVersion>$(MinorVersionPrefix)33</MinorVersion>
<PatchVersion>0</PatchVersion>
<PatchVersion>1</PatchVersion>
<BuildNumber Condition="'$(BuildNumber)' == '' ">0</BuildNumber>
<PreviewVersion></PreviewVersion>

Expand Down
5 changes: 2 additions & 3 deletions src/WebJobs.Script.WebHost/Diagnostics/SystemLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

// For Http function invocations we want to stamp invocation logs with
// the request ID for easy correlation with incoming Http request logs.
if (scopeProps.TryGetValue(ScriptConstants.LoggerHttpRequest, out scopeValue))
if (scopeProps.TryGetValue(ScriptConstants.AzureFunctionsRequestIdKey, out scopeValue))
{
var httpRequest = (HttpRequest)scopeValue;
scopeActivityId = httpRequest.GetRequestId();
scopeActivityId = scopeValue as string;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private async Task<IActionResult> GetResultAsync(HttpContext context, IFunctionE
var scopeState = new Dictionary<string, object>()
{
[ScriptConstants.LoggerHttpRequest] = context.Request,
[ScriptConstants.AzureFunctionsRequestIdKey] = context.Request.GetRequestId(),
};

using (logger.BeginScope(scopeState))
Expand Down
5 changes: 5 additions & 0 deletions src/WebJobs.Script/ScriptConstants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using NuGet.Versioning;

Expand Down Expand Up @@ -45,6 +46,10 @@ public static class ScriptConstants
public const string TraceSourceHttpHandler = "HttpRequestTraceHandler";
public const string TraceSourceHttpThrottleMiddleware = "HttpThrottleMiddleware";

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

public const string LogCategoryHostController = "Host.Controllers.Host";
Expand Down

0 comments on commit 3a214f2

Please sign in to comment.