Skip to content

Commit 9fd5b5e

Browse files
committed
Update Amazon.Lambda.Logging.AspNetCore to honor Lambda log config set to JSON
This means the log message sent through the ILogger is converted into a parameterized log message allowing RuntimeSupport to create the JSON document
1 parent 380a3ea commit 9fd5b5e

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

Libraries/src/Amazon.Lambda.Logging.AspNetCore/LambdaILogger.cs

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,63 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
5252
return;
5353
}
5454

55-
var components = new List<string>(4);
56-
if (_options.IncludeLogLevel)
57-
{
58-
components.Add($"[{logLevel}]");
59-
}
60-
61-
GetScopeInformation(components);
55+
var lambdaLogLevel = ConvertLogLevel(logLevel);
6256

63-
if (_options.IncludeCategory)
57+
if (IsLambdaJsonFormatEnabled && state is IEnumerable<KeyValuePair<string, object>> structure)
6458
{
65-
components.Add($"{_categoryName}:");
59+
string messageTemplate = null;
60+
var parameters = new List<object>();
61+
foreach (var property in structure)
62+
{
63+
if (property is { Key: "{OriginalFormat}", Value: string value })
64+
{
65+
messageTemplate = value;
66+
}
67+
else
68+
{
69+
parameters.Add(property.Value);
70+
}
71+
}
72+
73+
#pragma warning disable CA2252
74+
Amazon.Lambda.Core.LambdaLogger.Log(lambdaLogLevel, exception, messageTemplate, parameters.ToArray());
75+
#pragma warning restore CA2252
6676
}
67-
if (_options.IncludeEventId)
77+
else
6878
{
69-
components.Add($"[{eventId}]:");
70-
}
79+
var components = new List<string>(4);
80+
if (_options.IncludeLogLevel)
81+
{
82+
components.Add($"[{logLevel}]");
83+
}
7184

72-
var text = formatter.Invoke(state, exception);
73-
components.Add(text);
85+
GetScopeInformation(components);
7486

75-
if (_options.IncludeException)
76-
{
77-
components.Add($"{exception}");
78-
}
79-
if (_options.IncludeNewline)
80-
{
81-
components.Add(Environment.NewLine);
82-
}
87+
if (_options.IncludeCategory)
88+
{
89+
components.Add($"{_categoryName}:");
90+
}
91+
if (_options.IncludeEventId)
92+
{
93+
components.Add($"[{eventId}]:");
94+
}
8395

84-
var finalText = string.Join(" ", components);
96+
var text = formatter.Invoke(state, exception);
97+
components.Add(text);
8598

86-
var lambdaLogLevel = ConvertLogLevel(logLevel);
87-
Amazon.Lambda.Core.LambdaLogger.Log(lambdaLogLevel, finalText);
99+
if (_options.IncludeException)
100+
{
101+
components.Add($"{exception}");
102+
}
103+
if (_options.IncludeNewline)
104+
{
105+
components.Add(Environment.NewLine);
106+
}
107+
108+
var finalText = string.Join(" ", components);
109+
110+
Amazon.Lambda.Core.LambdaLogger.Log(lambdaLogLevel, finalText);
111+
}
88112
}
89113

90114
private static Amazon.Lambda.Core.LogLevel ConvertLogLevel(LogLevel logLevel)
@@ -126,9 +150,17 @@ private void GetScopeInformation(List<string> logMessageComponents)
126150
logMessageComponents.Add("=>");
127151
}
128152
}
129-
}
130-
131-
// Private classes
153+
}
154+
155+
private bool IsLambdaJsonFormatEnabled
156+
{
157+
get
158+
{
159+
return string.Equals(Environment.GetEnvironmentVariable("AWS_LAMBDA_LOG_FORMAT"), "JSON", StringComparison.InvariantCultureIgnoreCase);
160+
}
161+
}
162+
163+
// Private classes
132164
private class NoOpDisposable : IDisposable
133165
{
134166
public void Dispose()

0 commit comments

Comments
 (0)