-
Notifications
You must be signed in to change notification settings - Fork 432
/
ScriptLoggingBuilderExtensions.cs
69 lines (58 loc) · 2.59 KB
/
ScriptLoggingBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.WebJobs.Script.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
namespace Microsoft.Extensions.Logging
{
public static class ScriptLoggingBuilderExtensions
{
internal static readonly string[] AllowedCategoryPrefixes = new[]
{
"Microsoft.Azure.WebJobs",
"Function",
"Worker",
"Host"
};
public static void AddDefaultWebJobsFilters(this ILoggingBuilder builder)
{
builder.SetMinimumLevel(LogLevel.None);
builder.AddFilter((c, l) => Filter(c, l, LogLevel.Information));
}
public static void AddDefaultWebJobsFilters<T>(this ILoggingBuilder builder, LogLevel level) where T : ILoggerProvider
{
builder.AddFilter<T>(null, LogLevel.None);
builder.AddFilter<T>((c, l) => Filter(c, l, LogLevel.Trace));
}
private static bool Filter(string category, LogLevel actualLevel, LogLevel minLevel)
{
return actualLevel >= minLevel && AllowedCategoryPrefixes.Where(p => category.StartsWith(p)).Any();
}
public static void AddConsoleIfEnabled(this ILoggingBuilder builder, HostBuilderContext context)
{
AddConsoleIfEnabled(builder, context.HostingEnvironment.IsDevelopment(), context.Configuration);
}
public static void AddConsoleIfEnabled(this ILoggingBuilder builder, WebHostBuilderContext context)
{
AddConsoleIfEnabled(builder, context.HostingEnvironment.IsDevelopment(), context.Configuration);
}
private static void AddConsoleIfEnabled(ILoggingBuilder builder, bool isDevelopment, IConfiguration configuration)
{
// console logging defaults to false, except for self host
bool enableConsole = isDevelopment;
string consolePath = ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, "Logging", "Console", "IsEnabled");
IConfigurationSection configSection = configuration.GetSection(consolePath);
if (configSection.Exists())
{
// if it has been explicitly configured that value overrides default
enableConsole = configSection.Get<bool>();
}
if (enableConsole)
{
builder.AddConsole();
}
}
}
}