@@ -52,39 +52,63 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
52
52
return ;
53
53
}
54
54
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 ) ;
62
56
63
- if ( _options . IncludeCategory )
57
+ if ( IsLambdaJsonFormatEnabled && state is IEnumerable < KeyValuePair < string , object > > structure )
64
58
{
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
66
76
}
67
- if ( _options . IncludeEventId )
77
+ else
68
78
{
69
- components . Add ( $ "[{ eventId } ]:") ;
70
- }
79
+ var components = new List < string > ( 4 ) ;
80
+ if ( _options . IncludeLogLevel )
81
+ {
82
+ components . Add ( $ "[{ logLevel } ]") ;
83
+ }
71
84
72
- var text = formatter . Invoke ( state , exception ) ;
73
- components . Add ( text ) ;
85
+ GetScopeInformation ( components ) ;
74
86
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
+ }
83
95
84
- var finalText = string . Join ( " " , components ) ;
96
+ var text = formatter . Invoke ( state , exception ) ;
97
+ components . Add ( text ) ;
85
98
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
+ }
88
112
}
89
113
90
114
private static Amazon . Lambda . Core . LogLevel ConvertLogLevel ( LogLevel logLevel )
@@ -126,9 +150,17 @@ private void GetScopeInformation(List<string> logMessageComponents)
126
150
logMessageComponents . Add ( "=>" ) ;
127
151
}
128
152
}
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
132
164
private class NoOpDisposable : IDisposable
133
165
{
134
166
public void Dispose ( )
0 commit comments