|
1 | | -using System; |
| 1 | +using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 |
|
4 | 4 | namespace Microsoft.Extensions.Logging |
5 | 5 | { |
6 | | - internal class LambdaILogger : ILogger |
7 | | - { |
8 | | - // Private fields |
9 | | - private readonly string _categoryName; |
10 | | - private readonly LambdaLoggerOptions _options; |
11 | | - |
12 | | - |
13 | | - internal IExternalScopeProvider ScopeProvider { get; set; } |
14 | | - |
15 | | - // Constructor |
16 | | - public LambdaILogger(string categoryName, LambdaLoggerOptions options) |
17 | | - { |
18 | | - _categoryName = categoryName; |
19 | | - _options = options; |
20 | | - } |
21 | | - |
22 | | - // ILogger methods |
23 | | - public IDisposable BeginScope<TState>(TState state) => ScopeProvider?.Push(state) ?? new NoOpDisposable(); |
24 | | - |
25 | | - public bool IsEnabled(LogLevel logLevel) |
26 | | - { |
27 | | - return ( |
28 | | - _options.Filter == null || |
29 | | - _options.Filter(_categoryName, logLevel)); |
30 | | - } |
31 | | - |
32 | | - public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) |
33 | | - { |
34 | | - if (formatter == null) |
35 | | - { |
36 | | - throw new ArgumentNullException(nameof(formatter)); |
37 | | - } |
38 | | - |
39 | | - if (!IsEnabled(logLevel)) |
40 | | - { |
41 | | - return; |
42 | | - } |
43 | | - |
44 | | - // Format of the logged text, optional components are in {} |
45 | | - // {[LogLevel] }{ => Scopes : }{Category: }{EventId: }MessageText {Exception}{\n} |
46 | | - |
47 | | - var components = new List<string>(4); |
48 | | - if (_options.IncludeLogLevel) |
49 | | - { |
50 | | - components.Add($"[{logLevel}]"); |
51 | | - } |
52 | | - |
53 | | - GetScopeInformation(components); |
54 | | - |
55 | | - if (_options.IncludeCategory) |
56 | | - { |
57 | | - components.Add($"{_categoryName}:"); |
58 | | - } |
59 | | - if (_options.IncludeEventId) |
60 | | - { |
61 | | - components.Add($"[{eventId}]:"); |
62 | | - } |
63 | | - |
64 | | - var text = formatter.Invoke(state, exception); |
65 | | - components.Add(text); |
66 | | - |
67 | | - if (_options.IncludeException) |
68 | | - { |
69 | | - components.Add($"{exception}"); |
70 | | - } |
71 | | - if (_options.IncludeNewline) |
72 | | - { |
73 | | - components.Add(Environment.NewLine); |
74 | | - } |
75 | | - |
76 | | - var finalText = string.Join(" ", components); |
77 | | - Amazon.Lambda.Core.LambdaLogger.Log(finalText); |
78 | | - } |
79 | | - |
80 | | - private void GetScopeInformation(List<string> logMessageComponents) |
81 | | - { |
82 | | - var scopeProvider = ScopeProvider; |
83 | | - |
84 | | - if (_options.IncludeScopes && scopeProvider != null) |
85 | | - { |
86 | | - var initialCount = logMessageComponents.Count; |
87 | | - |
88 | | - scopeProvider.ForEachScope((scope, list) => |
89 | | - { |
90 | | - list.Add(scope.ToString()); |
91 | | - }, (logMessageComponents)); |
92 | | - |
93 | | - if (logMessageComponents.Count > initialCount) |
94 | | - { |
95 | | - logMessageComponents.Add("=>"); |
96 | | - } |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - // Private classes |
101 | | - private class NoOpDisposable : IDisposable |
102 | | - { |
103 | | - public void Dispose() |
104 | | - { |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - } |
| 6 | + internal class LambdaILogger : ILogger |
| 7 | + { |
| 8 | + // Private fields |
| 9 | + private readonly string _categoryName; |
| 10 | + private readonly LambdaLoggerOptions _options; |
| 11 | + |
| 12 | + |
| 13 | + internal IExternalScopeProvider ScopeProvider { get; set; } |
| 14 | + |
| 15 | + // Constructor |
| 16 | + public LambdaILogger(string categoryName, LambdaLoggerOptions options) |
| 17 | + { |
| 18 | + _categoryName = categoryName; |
| 19 | + _options = options; |
| 20 | + } |
| 21 | + |
| 22 | + // ILogger methods |
| 23 | + public IDisposable BeginScope<TState>(TState state) => ScopeProvider?.Push(state) ?? new NoOpDisposable(); |
| 24 | + |
| 25 | + public bool IsEnabled(LogLevel logLevel) |
| 26 | + { |
| 27 | + return ( |
| 28 | + _options.Filter == null || |
| 29 | + _options.Filter(_categoryName, logLevel)); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// The Log method called by the ILogger framework to log message to logger's target. In the Lambda case the formatted logging will be |
| 34 | + /// sent to the Amazon.Lambda.Core.LambdaLogger's Log method. |
| 35 | + /// </summary> |
| 36 | + /// <typeparam name="TState"></typeparam> |
| 37 | + /// <param name="logLevel"></param> |
| 38 | + /// <param name="eventId"></param> |
| 39 | + /// <param name="state"></param> |
| 40 | + /// <param name="exception"></param> |
| 41 | + /// <param name="formatter"></param> |
| 42 | + /// <exception cref="ArgumentNullException"></exception> |
| 43 | + public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) |
| 44 | + { |
| 45 | + if (formatter == null) |
| 46 | + { |
| 47 | + throw new ArgumentNullException(nameof(formatter)); |
| 48 | + } |
| 49 | + |
| 50 | + if (!IsEnabled(logLevel)) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + var components = new List<string>(4); |
| 56 | + if (_options.IncludeLogLevel) |
| 57 | + { |
| 58 | + components.Add($"[{logLevel}]"); |
| 59 | + } |
| 60 | + |
| 61 | + GetScopeInformation(components); |
| 62 | + |
| 63 | + if (_options.IncludeCategory) |
| 64 | + { |
| 65 | + components.Add($"{_categoryName}:"); |
| 66 | + } |
| 67 | + if (_options.IncludeEventId) |
| 68 | + { |
| 69 | + components.Add($"[{eventId}]:"); |
| 70 | + } |
| 71 | + |
| 72 | + var text = formatter.Invoke(state, exception); |
| 73 | + components.Add(text); |
| 74 | + |
| 75 | + if (_options.IncludeException) |
| 76 | + { |
| 77 | + components.Add($"{exception}"); |
| 78 | + } |
| 79 | + if (_options.IncludeNewline) |
| 80 | + { |
| 81 | + components.Add(Environment.NewLine); |
| 82 | + } |
| 83 | + |
| 84 | + var finalText = string.Join(" ", components); |
| 85 | + |
| 86 | + var lambdaLogLevel = ConvertLogLevel(logLevel); |
| 87 | + Amazon.Lambda.Core.LambdaLogger.Log(lambdaLogLevel, finalText); |
| 88 | + } |
| 89 | + |
| 90 | + private static Amazon.Lambda.Core.LogLevel ConvertLogLevel(LogLevel logLevel) |
| 91 | + { |
| 92 | + switch (logLevel) |
| 93 | + { |
| 94 | + case LogLevel.Trace: |
| 95 | + return Amazon.Lambda.Core.LogLevel.Trace; |
| 96 | + case LogLevel.Debug: |
| 97 | + return Amazon.Lambda.Core.LogLevel.Debug; |
| 98 | + case LogLevel.Information: |
| 99 | + return Amazon.Lambda.Core.LogLevel.Information; |
| 100 | + case LogLevel.Warning: |
| 101 | + return Amazon.Lambda.Core.LogLevel.Warning; |
| 102 | + case LogLevel.Error: |
| 103 | + return Amazon.Lambda.Core.LogLevel.Error; |
| 104 | + case LogLevel.Critical: |
| 105 | + return Amazon.Lambda.Core.LogLevel.Critical; |
| 106 | + default: |
| 107 | + return Amazon.Lambda.Core.LogLevel.Information; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private void GetScopeInformation(List<string> logMessageComponents) |
| 112 | + { |
| 113 | + var scopeProvider = ScopeProvider; |
| 114 | + |
| 115 | + if (_options.IncludeScopes && scopeProvider != null) |
| 116 | + { |
| 117 | + var initialCount = logMessageComponents.Count; |
| 118 | + |
| 119 | + scopeProvider.ForEachScope((scope, list) => |
| 120 | + { |
| 121 | + list.Add(scope.ToString()); |
| 122 | + }, (logMessageComponents)); |
| 123 | + |
| 124 | + if (logMessageComponents.Count > initialCount) |
| 125 | + { |
| 126 | + logMessageComponents.Add("=>"); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Private classes |
| 132 | + private class NoOpDisposable : IDisposable |
| 133 | + { |
| 134 | + public void Dispose() |
| 135 | + { |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + } |
109 | 140 | } |
0 commit comments