Skip to content

Commit 7803096

Browse files
authored
Add hosting config support to control 3.x log disablement (#10552)
* Add RestrictHostLogs config * Utilize RestrictHostLogs flag for logging filters * Update tests * Refactor * Remove accidental dup * Change default behaviour * Update release notes * Set TreatWarningsAsErrors to false in test project * Fix integration tests * Skip EventHubTrigger E2E test due to auth changes
1 parent 1b946ba commit 7803096

File tree

15 files changed

+237
-51
lines changed

15 files changed

+237
-51
lines changed

release_notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
-->
55
*Please note we have reached end-of-life (EOL) support for v3.x.* For more information on supported runtime versions, please see [here.](https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=v4&pivots=programming-language-csharp)
66

7-
- Disable all non host startup logs for v3.x (#10399)
7+
- Disable all non host startup logs for v3.x (#10399)
8+
- Add hosting config (`DisableHostLogs`) to enable/disable v3.x logs (#10552)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace Microsoft.Extensions.Logging
88
{
99
public static class ILoggingBuilderExtensions
1010
{
11-
public static void AddWebJobsSystem<T>(this ILoggingBuilder builder) where T : SystemLoggerProvider
11+
public static void AddWebJobsSystem<T>(this ILoggingBuilder builder, bool restrictHostLogs = false) where T : SystemLoggerProvider
1212
{
1313
builder.Services.AddSingleton<ILoggerProvider, T>();
1414

1515
// Log all logs to SystemLogger
16-
builder.AddDefaultWebJobsFilters<T>(LogLevel.Trace);
16+
builder.AddDefaultWebJobsFilters<T>(LogLevel.Trace, restrictHostLogs);
1717
}
1818
}
1919
}

src/WebJobs.Script.WebHost/WebScriptHostBuilderExtension.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ public static IHostBuilder AddWebScriptHost(this IHostBuilder builder, IServiceP
7575
})
7676
.ConfigureLogging(loggingBuilder =>
7777
{
78+
var hostingConfigOptions = rootServiceProvider.GetService<IOptions<FunctionsHostingConfigOptions>>();
79+
var restrictHostLogs = RestrictHostLogs(hostingConfigOptions.Value);
80+
7881
loggingBuilder.Services.AddSingleton<ILoggerFactory, ScriptLoggerFactory>();
82+
loggingBuilder.AddWebJobsSystem<SystemLoggerProvider>(restrictHostLogs);
7983

80-
loggingBuilder.AddWebJobsSystem<SystemLoggerProvider>();
8184
if (environment.IsAzureMonitorEnabled())
8285
{
8386
loggingBuilder.Services.AddSingleton<ILoggerProvider, AzureMonitorDiagnosticLoggerProvider>();
@@ -170,6 +173,12 @@ private static void ConfigureRegisteredBuilders<TBuilder>(TBuilder builder, ISer
170173
}
171174
}
172175

176+
private static bool RestrictHostLogs(FunctionsHostingConfigOptions options)
177+
{
178+
// Feature flag should take precedence over the host configuration
179+
return !FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagEnableHostLogs) && options.RestrictHostLogs;
180+
}
181+
173182
/// <summary>
174183
/// Used internally to register Newtonsoft formatters with our ScriptHost.
175184
/// </summary>

src/WebJobs.Script/Config/FunctionsHostingConfigOptions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ public bool SwtIssuerEnabled
8585
}
8686
}
8787

88+
/// <summary>
89+
/// Gets or sets a value indicating whether non-critical logs should be disabled in the host.
90+
/// </summary>
91+
public bool RestrictHostLogs
92+
{
93+
get
94+
{
95+
return GetFeatureOrDefault(ScriptConstants.HostingConfigRestrictHostLogs, "0") == "1";
96+
}
97+
98+
set
99+
{
100+
_features[ScriptConstants.HostingConfigRestrictHostLogs] = value ? "1" : "0";
101+
}
102+
}
103+
88104
/// <summary>
89105
/// Gets feature by name.
90106
/// </summary>

src/WebJobs.Script/Extensions/ScriptLoggingBuilderExtensions.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Linq;
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.Azure.WebJobs.Script;
9-
using Microsoft.Azure.WebJobs.Script.Config;
109
using Microsoft.Azure.WebJobs.Script.Configuration;
1110
using Microsoft.Extensions.Configuration;
1211
using Microsoft.Extensions.Hosting;
@@ -15,17 +14,25 @@ namespace Microsoft.Extensions.Logging
1514
{
1615
public static class ScriptLoggingBuilderExtensions
1716
{
18-
private static ConcurrentDictionary<string, bool> _filteredCategoryCache = new ConcurrentDictionary<string, bool>();
17+
private static ConcurrentDictionary<string, bool> _filteredCategoryCache = new ();
18+
private static ImmutableArray<string> _allowedLogCategoryPrefixes = ImmutableArray<string>.Empty;
1919

20-
public static ILoggingBuilder AddDefaultWebJobsFilters(this ILoggingBuilder builder)
20+
// For testing only
21+
internal static ImmutableArray<string> AllowedSystemLogPrefixes => _allowedLogCategoryPrefixes;
22+
23+
public static ILoggingBuilder AddDefaultWebJobsFilters(this ILoggingBuilder builder, bool restrictHostLogs = false)
2124
{
25+
SetSystemLogCategoryPrefixes(restrictHostLogs);
26+
2227
builder.SetMinimumLevel(LogLevel.None);
2328
builder.AddFilter((c, l) => Filter(c, l, LogLevel.Information));
2429
return builder;
2530
}
2631

27-
public static ILoggingBuilder AddDefaultWebJobsFilters<T>(this ILoggingBuilder builder, LogLevel level) where T : ILoggerProvider
32+
public static ILoggingBuilder AddDefaultWebJobsFilters<T>(this ILoggingBuilder builder, LogLevel level, bool restrictHostLogs = false) where T : ILoggerProvider
2833
{
34+
SetSystemLogCategoryPrefixes(restrictHostLogs);
35+
2936
builder.AddFilter<T>(null, LogLevel.None);
3037
builder.AddFilter<T>((c, l) => Filter(c, l, level));
3138
return builder;
@@ -38,11 +45,18 @@ internal static bool Filter(string category, LogLevel actualLevel, LogLevel minL
3845

3946
private static bool IsFiltered(string category)
4047
{
41-
ImmutableArray<string> systemLogCategoryPrefixes = FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagEnableHostLogs)
42-
? ScriptConstants.SystemLogCategoryPrefixes
43-
: ScriptConstants.RestrictedSystemLogCategoryPrefixes;
48+
return _filteredCategoryCache.GetOrAdd(category, c => _allowedLogCategoryPrefixes.Any(p => c.StartsWith(p)));
49+
}
50+
51+
private static void SetSystemLogCategoryPrefixes(bool restrictHostLogs)
52+
{
53+
var previous = _allowedLogCategoryPrefixes;
54+
_allowedLogCategoryPrefixes = restrictHostLogs ? ScriptConstants.RestrictedSystemLogCategoryPrefixes : ScriptConstants.SystemLogCategoryPrefixes;
4455

45-
return _filteredCategoryCache.GetOrAdd(category, c => systemLogCategoryPrefixes.Any(p => category.StartsWith(p)));
56+
if (!previous.IsDefault && !previous.SequenceEqual(_allowedLogCategoryPrefixes))
57+
{
58+
_filteredCategoryCache.Clear();
59+
}
4660
}
4761

4862
public static void AddConsoleIfEnabled(this ILoggingBuilder builder, HostBuilderContext context)

src/WebJobs.Script/ScriptConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public static class ScriptConstants
127127
public const string FeatureFlagEnableHostLogs = "EnableHostLogs";
128128
public const string HostingConfigSwtAuthenticationEnabled = "SwtAuthenticationEnabled";
129129
public const string HostingConfigSwtIssuerEnabled = "SwtIssuerEnabled";
130+
public const string HostingConfigRestrictHostLogs = "RestrictHostLogs";
130131

131132
public const string SiteAzureFunctionsUriFormat = "https://{0}.azurewebsites.net/azurefunctions";
132133
public const string ScmSiteUriFormat = "https://{0}.scm.azurewebsites.net";

test/TestFunctions/TestFunctions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
88
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
9-
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
9+
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
1010
<WarningsAsErrors />
1111
</PropertyGroup>
1212

test/WebJobs.Script.Tests.Integration/EventHubs/EventHubsEndToEndTestsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public EventHubsEndToEndTestsBase(TestFixture fixture)
2121
_fixture = fixture;
2222
}
2323

24-
[Fact]
24+
[Fact(Skip = "SAS authentication has been disabled for the namespace.")]
2525
public async Task EventHub()
2626
{
2727
// Event Hub needs the following environment vars:

test/WebJobs.Script.Tests.Integration/Management/InstanceManagerTests.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public async Task ValidateContext_InvalidZipUrl_WebsiteUseZip_ReturnsError()
356356
{
357357
var environmentSettings = new Dictionary<string, string>()
358358
{
359-
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, "http://invalid.com/invalid/dne" }
359+
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, "http://invalid.test/invalid/dne" }
360360
};
361361

362362
var environment = new TestEnvironment();
@@ -439,7 +439,7 @@ public async Task ValidateContext_Succeeds_For_WebsiteUseZip_Only()
439439
{
440440
var environment = new Dictionary<string, string>()
441441
{
442-
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, "http://microsoft.com" }
442+
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, "https://valid-zip.test" }
443443
};
444444
var assignmentContext = new HostAssignmentContext
445445
{
@@ -449,7 +449,19 @@ public async Task ValidateContext_Succeeds_For_WebsiteUseZip_Only()
449449
IsWarmupRequest = false
450450
};
451451

452-
string error = await _instanceManager.ValidateContext(assignmentContext);
452+
var handlerMock = new Mock<HttpMessageHandler>();
453+
handlerMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync",
454+
ItExpr.IsAny<HttpRequestMessage>(),
455+
ItExpr.IsAny<CancellationToken>()).ReturnsAsync(new HttpResponseMessage
456+
{
457+
StatusCode = HttpStatusCode.OK,
458+
});
459+
460+
var instanceManager = new InstanceManager(_optionsFactory, TestHelpers.CreateHttpClientFactory(handlerMock.Object).CreateClient(),
461+
_scriptWebEnvironment, _environment, _loggerFactory.CreateLogger<InstanceManager>(),
462+
new TestMetricsLogger(), null, _runFromPackageHandler, _packageDownloadHandler.Object);
463+
464+
string error = await instanceManager.ValidateContext(assignmentContext);
453465
Assert.Null(error);
454466

455467
string[] expectedOutputLines =
@@ -539,8 +551,8 @@ public async Task ValidateContext_Succeeds_For_WebsiteUseZip_With_ScmPackageDefi
539551
{
540552
var environment = new Dictionary<string, string>()
541553
{
542-
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, "http://microsoft.com" },
543-
{ EnvironmentSettingNames.ScmRunFromPackage, "http://microsoft.com" }
554+
{ EnvironmentSettingNames.AzureWebsiteZipDeployment, "https://valid.test" },
555+
{ EnvironmentSettingNames.ScmRunFromPackage, "https://valid.tests" }
544556
};
545557
var assignmentContext = new HostAssignmentContext
546558
{
@@ -550,7 +562,19 @@ public async Task ValidateContext_Succeeds_For_WebsiteUseZip_With_ScmPackageDefi
550562
IsWarmupRequest = false
551563
};
552564

553-
string error = await _instanceManager.ValidateContext(assignmentContext);
565+
var handlerMock = new Mock<HttpMessageHandler>();
566+
handlerMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync",
567+
ItExpr.IsAny<HttpRequestMessage>(),
568+
ItExpr.IsAny<CancellationToken>()).ReturnsAsync(new HttpResponseMessage
569+
{
570+
StatusCode = HttpStatusCode.OK,
571+
});
572+
573+
var instanceManager = new InstanceManager(_optionsFactory, TestHelpers.CreateHttpClientFactory(handlerMock.Object).CreateClient(),
574+
_scriptWebEnvironment, _environment, _loggerFactory.CreateLogger<InstanceManager>(),
575+
new TestMetricsLogger(), null, _runFromPackageHandler, _packageDownloadHandler.Object);
576+
577+
string error = await instanceManager.ValidateContext(assignmentContext);
554578
Assert.Null(error);
555579

556580
string[] expectedOutputLines =

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/SamplesEndToEndTests_Node.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public SamplesEndToEndTests_Node(TestFixture fixture)
3939
_settingsManager = ScriptSettingsManager.Instance;
4040
}
4141

42-
[Fact]
42+
[Fact(Skip = "SAS authentication has been disabled for the namespace.")]
4343
public async Task EventHubTrigger()
4444
{
4545
// write 3 events

0 commit comments

Comments
 (0)