-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathLambdaILogger.cs
More file actions
175 lines (149 loc) · 5.81 KB
/
LambdaILogger.cs
File metadata and controls
175 lines (149 loc) · 5.81 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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 lambdaLogLevel = ConvertLogLevel(logLevel);
if (IsLambdaJsonFormatEnabled && state is IEnumerable<KeyValuePair<string, object>> structure)
{
string messageTemplate = null;
var parameters = new List<object>();
foreach (var property in structure)
{
if (property is { Key: "{OriginalFormat}", Value: string value })
{
messageTemplate = value;
}
else
{
parameters.Add(property.Value);
}
}
if (messageTemplate == null)
{
messageTemplate = formatter.Invoke(state, exception);
}
Amazon.Lambda.Core.LambdaLogger.Log(lambdaLogLevel, exception, messageTemplate, parameters.ToArray());
}
else
{
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);
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 bool IsLambdaJsonFormatEnabled
{
get
{
return string.Equals(Environment.GetEnvironmentVariable("AWS_LAMBDA_LOG_FORMAT"), "JSON", StringComparison.InvariantCultureIgnoreCase);
}
}
// Private classes
private class NoOpDisposable : IDisposable
{
public void Dispose()
{
}
}
}
}