-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathLambdaILogger.cs
More file actions
140 lines (118 loc) · 4.13 KB
/
LambdaILogger.cs
File metadata and controls
140 lines (118 loc) · 4.13 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
namespace Microsoft.Extensions.Logging
{
internal class LambdaILogger : ILogger
{
// Private fields
private readonly string _categoryName;
private readonly LambdaLoggerOptions _options;
internal IExternalScopeProvider ScopeProvider { get; set; }
// Constructor
public LambdaILogger(string categoryName, LambdaLoggerOptions options)
{
_categoryName = categoryName;
_options = options;
}
// ILogger methods
public IDisposable BeginScope<TState>(TState state) => ScopeProvider?.Push(state) ?? new NoOpDisposable();
public bool IsEnabled(LogLevel logLevel)
{
return (
_options.Filter == null ||
_options.Filter(_categoryName, logLevel));
}
/// <summary>
/// The Log method called by the ILogger framework to log message to logger's target. In the Lambda case the formatted logging will be
/// sent to the Amazon.Lambda.Core.LambdaLogger's Log method.
/// </summary>
/// <typeparam name="TState"></typeparam>
/// <param name="logLevel"></param>
/// <param name="eventId"></param>
/// <param name="state"></param>
/// <param name="exception"></param>
/// <param name="formatter"></param>
/// <exception cref="ArgumentNullException"></exception>
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (formatter == null)
{
throw new ArgumentNullException(nameof(formatter));
}
if (!IsEnabled(logLevel))
{
return;
}
var components = new List<string>(4);
if (_options.IncludeLogLevel)
{
components.Add($"[{logLevel}]");
}
GetScopeInformation(components);
if (_options.IncludeCategory)
{
components.Add($"{_categoryName}:");
}
if (_options.IncludeEventId)
{
components.Add($"[{eventId}]:");
}
var text = formatter.Invoke(state, exception);
components.Add(text);
if (_options.IncludeException)
{
components.Add($"{exception}");
}
if (_options.IncludeNewline)
{
components.Add(Environment.NewLine);
}
var finalText = string.Join(" ", components);
var lambdaLogLevel = ConvertLogLevel(logLevel);
Amazon.Lambda.Core.LambdaLogger.Log(lambdaLogLevel, finalText);
}
private static Amazon.Lambda.Core.LogLevel ConvertLogLevel(LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel.Trace:
return Amazon.Lambda.Core.LogLevel.Trace;
case LogLevel.Debug:
return Amazon.Lambda.Core.LogLevel.Debug;
case LogLevel.Information:
return Amazon.Lambda.Core.LogLevel.Information;
case LogLevel.Warning:
return Amazon.Lambda.Core.LogLevel.Warning;
case LogLevel.Error:
return Amazon.Lambda.Core.LogLevel.Error;
case LogLevel.Critical:
return Amazon.Lambda.Core.LogLevel.Critical;
default:
return Amazon.Lambda.Core.LogLevel.Information;
}
}
private void GetScopeInformation(List<string> logMessageComponents)
{
var scopeProvider = ScopeProvider;
if (_options.IncludeScopes && scopeProvider != null)
{
var initialCount = logMessageComponents.Count;
scopeProvider.ForEachScope((scope, list) =>
{
list.Add(scope.ToString());
}, (logMessageComponents));
if (logMessageComponents.Count > initialCount)
{
logMessageComponents.Add("=>");
}
}
}
// Private classes
private class NoOpDisposable : IDisposable
{
public void Dispose()
{
}
}
}
}