Repository navigation
Expand file tree
/
Copy pathLoggerRuleSelector.cs
More file actions
100 lines (88 loc) · 3.48 KB
/
Copy pathLoggerRuleSelector.cs
File metadata and controls
100 lines (88 loc) · 3.48 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* ===============================================
* 功能描述:AspNetCore.Logging.LoggerRuleSelector
* 创 建 者:WeiGe
* 创建日期:1/2/2019 9:51:28 PM
* ===============================================*/
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
namespace AspNetCore.FileLog
{
internal class LoggerRuleSelector
{
public void Select(LoggerFilterOptions options, Type providerType, string category,out LogType logType, out LogScope logScope,
out LogLevel? minLevel,out int traceCount, out Func<string, string, LogLevel, bool> filter)
{
filter = null;
minLevel = options.MinLevel;
logScope = options.MiniScope;
traceCount = options.TraceCount;
logType = options.LogType;
// Filter rule selection:
// 1. Select rules for current logger type, if there is none, select ones without logger type specified
// 2. Select rules with longest matching categories
// 3. If there nothing matched by category take all rules without category
// 3. If there is only one rule use it's level and filter
// 4. If there are multiple rules use last
// 5. If there are no applicable rules use global minimal level
var providerAlias = LoggerProviderAliasUtilities.GetAlias(providerType);
LoggerFilterRule current = null;
foreach (LoggerFilterRule rule in options.Rules)
{
if (IsBetter(rule, current, providerType.FullName, category)
|| (!string.IsNullOrEmpty(providerAlias) && IsBetter(rule, current, providerAlias, category)))
{
current = rule;
}
}
if (current != null)
{
filter = current.Filter;
minLevel = current.LogLevel;
logScope = current.LogScope;
traceCount = current.TraceCount;
logType = current.LogType;
}
}
private static bool IsBetter(LoggerFilterRule rule, LoggerFilterRule current, string logger, string category)
{
// Skip rules with inapplicable type or category
if (rule.ProviderName != null && rule.ProviderName != logger)
{
return false;
}
if (rule.CategoryName != null && !category.StartsWith(rule.CategoryName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (current?.ProviderName != null)
{
if (rule.ProviderName == null)
{
return false;
}
}
else
{
// We want to skip category check when going from no provider to having provider
if (rule.ProviderName != null)
{
return true;
}
}
if (current?.CategoryName != null)
{
if (rule.CategoryName == null)
{
return false;
}
if (current.CategoryName.Length > rule.CategoryName.Length)
{
return false;
}
}
return true;
}
}
}