@@ -52,39 +52,61 @@ 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+ Amazon . Lambda . Core . LambdaLogger . Log ( lambdaLogLevel , exception , messageTemplate , parameters . ToArray ( ) ) ;
6674 }
67- if ( _options . IncludeEventId )
75+ else
6876 {
69- components . Add ( $ "[{ eventId } ]:") ;
70- }
77+ var components = new List < string > ( 4 ) ;
78+ if ( _options . IncludeLogLevel )
79+ {
80+ components . Add ( $ "[{ logLevel } ]") ;
81+ }
7182
72- var text = formatter . Invoke ( state , exception ) ;
73- components . Add ( text ) ;
83+ GetScopeInformation ( components ) ;
7484
75- if ( _options . IncludeException )
76- {
77- components . Add ( $ "{ exception } ") ;
78- }
79- if ( _options . IncludeNewline )
80- {
81- components . Add ( Environment . NewLine ) ;
82- }
85+ if ( _options . IncludeCategory )
86+ {
87+ components . Add ( $ "{ _categoryName } : ") ;
88+ }
89+ if ( _options . IncludeEventId )
90+ {
91+ components . Add ( $ "[ { eventId } ]:" ) ;
92+ }
8393
84- var finalText = string . Join ( " " , components ) ;
94+ var text = formatter . Invoke ( state , exception ) ;
95+ components . Add ( text ) ;
8596
86- var lambdaLogLevel = ConvertLogLevel ( logLevel ) ;
87- Amazon . Lambda . Core . LambdaLogger . Log ( lambdaLogLevel , finalText ) ;
97+ if ( _options . IncludeException )
98+ {
99+ components . Add ( $ "{ exception } ") ;
100+ }
101+ if ( _options . IncludeNewline )
102+ {
103+ components . Add ( Environment . NewLine ) ;
104+ }
105+
106+ var finalText = string . Join ( " " , components ) ;
107+
108+ Amazon . Lambda . Core . LambdaLogger . Log ( lambdaLogLevel , finalText ) ;
109+ }
88110 }
89111
90112 private static Amazon . Lambda . Core . LogLevel ConvertLogLevel ( LogLevel logLevel )
@@ -126,9 +148,17 @@ private void GetScopeInformation(List<string> logMessageComponents)
126148 logMessageComponents . Add ( "=>" ) ;
127149 }
128150 }
129- }
130-
131- // Private classes
151+ }
152+
153+ private bool IsLambdaJsonFormatEnabled
154+ {
155+ get
156+ {
157+ return string . Equals ( Environment . GetEnvironmentVariable ( "AWS_LAMBDA_LOG_FORMAT" ) , "JSON" , StringComparison . InvariantCultureIgnoreCase ) ;
158+ }
159+ }
160+
161+ // Private classes
132162 private class NoOpDisposable : IDisposable
133163 {
134164 public void Dispose ( )
0 commit comments