Skip to content

Commit

Permalink
Utilize RestrictHostLogs flag for logging filters
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem committed Oct 31, 2024
1 parent f765d95 commit e65a5a4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Microsoft.Extensions.Logging
{
public static class ILoggingBuilderExtensions
{
public static void AddWebJobsSystem<T>(this ILoggingBuilder builder) where T : SystemLoggerProvider
public static void AddWebJobsSystem<T>(this ILoggingBuilder builder, bool restrictHostLogs = false) where T : SystemLoggerProvider
{
builder.Services.AddSingleton<ILoggerProvider, T>();

// Log all logs to SystemLogger
builder.AddDefaultWebJobsFilters<T>(LogLevel.Trace);
builder.AddDefaultWebJobsFilters<T>(LogLevel.Trace, restrictHostLogs);
}
}
}
27 changes: 20 additions & 7 deletions src/WebJobs.Script/Extensions/ScriptLoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.WebJobs.Script;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
Expand All @@ -16,16 +15,24 @@ namespace Microsoft.Extensions.Logging
public static class ScriptLoggingBuilderExtensions
{
private static ConcurrentDictionary<string, bool> _filteredCategoryCache = new ConcurrentDictionary<string, bool>();
private static ImmutableArray<string> _allowedLogCategoryPrefixes = ScriptConstants.SystemLogCategoryPrefixes;

public static ILoggingBuilder AddDefaultWebJobsFilters(this ILoggingBuilder builder)
// For testing only
internal static ImmutableArray<string> AllowedSystemLogPrefixes => _allowedLogCategoryPrefixes;

public static ILoggingBuilder AddDefaultWebJobsFilters(this ILoggingBuilder builder, bool restrictHostLogs = false)
{
SetSystemLogCategoryPrefixes(restrictHostLogs);

builder.SetMinimumLevel(LogLevel.None);
builder.AddFilter((c, l) => Filter(c, l, LogLevel.Information));
return builder;
}

public static ILoggingBuilder AddDefaultWebJobsFilters<T>(this ILoggingBuilder builder, LogLevel level) where T : ILoggerProvider
public static ILoggingBuilder AddDefaultWebJobsFilters<T>(this ILoggingBuilder builder, LogLevel level, bool restrictHostLogs = false) where T : ILoggerProvider
{
SetSystemLogCategoryPrefixes(restrictHostLogs);

builder.AddFilter<T>(null, LogLevel.None);
builder.AddFilter<T>((c, l) => Filter(c, l, level));
return builder;
Expand All @@ -38,11 +45,17 @@ internal static bool Filter(string category, LogLevel actualLevel, LogLevel minL

private static bool IsFiltered(string category)
{
ImmutableArray<string> systemLogCategoryPrefixes = FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagEnableHostLogs)
? ScriptConstants.SystemLogCategoryPrefixes
: ScriptConstants.RestrictedSystemLogCategoryPrefixes;
return _filteredCategoryCache.GetOrAdd(category, c => _allowedLogCategoryPrefixes.Any(p => c.StartsWith(p)));
}

return _filteredCategoryCache.GetOrAdd(category, c => systemLogCategoryPrefixes.Any(p => category.StartsWith(p)));
private static void SetSystemLogCategoryPrefixes(bool restrictHostLogs)
{
// Once restrictHostLogs is set to true, it stays true for the rest of the application's lifetime
// Set _allowedLogCategoryPrefixes to the restricted prefixes and clear the filter cache
if (restrictHostLogs)
{
_allowedLogCategoryPrefixes = ScriptConstants.RestrictedSystemLogCategoryPrefixes;
}
}

public static void AddConsoleIfEnabled(this ILoggingBuilder builder, HostBuilderContext context)
Expand Down
10 changes: 9 additions & 1 deletion src/WebJobs.Script/ScriptHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public static IHostBuilder AddScriptHost(this IHostBuilder builder,
// Host configuration
builder.ConfigureLogging((context, loggingBuilder) =>
{
loggingBuilder.AddDefaultWebJobsFilters();
var hostingConfigOptions = applicationOptions.RootServiceProvider.GetService<IOptions<FunctionsHostingConfigOptions>>();
var restrictHostLogs = RestrictHostLogs(hostingConfigOptions.Value, SystemEnvironment.Instance);
loggingBuilder.AddDefaultWebJobsFilters(restrictHostLogs);

string loggingPath = ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "Logging");
loggingBuilder.AddConfiguration(context.Configuration.GetSection(loggingPath));
Expand Down Expand Up @@ -492,6 +494,12 @@ private static IDistributedLockManager GetBlobLockManager(IServiceProvider provi
}
}

private static bool RestrictHostLogs(FunctionsHostingConfigOptions options, IEnvironment environment)
{
// Feature flag should take precedence over the host configuration
return !FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagEnableHostLogs, environment) && options.RestrictHostLogs;
}

/// <summary>
/// Gets and removes the specified value, if it exists and is of type T.
/// Throws an InvalidOperationException if the key does not exist or is not of type T.
Expand Down

0 comments on commit e65a5a4

Please sign in to comment.