@@ -65,29 +65,57 @@ private static bool UseLoggerMessageDefine(LoggerMethod lm)
6565
6666 private void GenType ( LoggerClass lc )
6767 {
68+ string nestedIndentation = "" ;
6869 if ( ! string . IsNullOrWhiteSpace ( lc . Namespace ) )
6970 {
7071 _builder . Append ( $@ "
7172namespace { lc . Namespace }
7273{{" ) ;
7374 }
7475
76+ LoggerClass parent = lc . ParentClass ;
77+ var parentClasses = new List < string > ( ) ;
78+ // loop until you find top level nested class
79+ while ( parent != null )
80+ {
81+ parentClasses . Add ( $ "partial { parent . Keyword } { parent . Name } { parent . Constraints } ") ;
82+ parent = parent . ParentClass ;
83+ }
84+
85+ // write down top level nested class first
86+ for ( int i = parentClasses . Count - 1 ; i >= 0 ; i -- )
87+ {
88+ _builder . Append ( $@ "
89+ { nestedIndentation } { parentClasses [ i ] }
90+ { nestedIndentation } {{" ) ;
91+ nestedIndentation += " " ;
92+ }
93+
7594 _builder . Append ( $@ "
76- partial class { lc . Name } { lc . Constraints }
77- {{" ) ;
95+ { nestedIndentation } partial { lc . Keyword } { lc . Name } { lc . Constraints }
96+ { nestedIndentation } { {" ) ;
7897
7998 foreach ( LoggerMethod lm in lc . Methods )
8099 {
81100 if ( ! UseLoggerMessageDefine ( lm ) )
82101 {
83- GenStruct ( lm ) ;
102+ GenStruct ( lm , nestedIndentation ) ;
84103 }
85104
86- GenLogMethod ( lm ) ;
105+ GenLogMethod ( lm , nestedIndentation ) ;
87106 }
88107
89108 _builder . Append ( $@ "
90- }}" ) ;
109+ { nestedIndentation } }}" ) ;
110+
111+ parent = lc . ParentClass ;
112+ while ( parent != null )
113+ {
114+ nestedIndentation = new String ( ' ' , nestedIndentation . Length - 4 ) ;
115+ _builder . Append ( $@ "
116+ { nestedIndentation } }}" ) ;
117+ parent = parent . ParentClass ;
118+ }
91119
92120 if ( ! string . IsNullOrWhiteSpace ( lc . Namespace ) )
93121 {
@@ -96,83 +124,83 @@ partial class {lc.Name} {lc.Constraints}
96124 }
97125 }
98126
99- private void GenStruct ( LoggerMethod lm )
127+ private void GenStruct ( LoggerMethod lm , string nestedIndentation )
100128 {
101129 _builder . AppendLine ( $@ "
102- [{ s_generatedCodeAttribute } ]
103- private readonly struct __{ lm . Name } Struct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>>
104- {{" ) ;
105- GenFields ( lm ) ;
130+ { nestedIndentation } [{ s_generatedCodeAttribute } ]
131+ { nestedIndentation } private readonly struct __{ lm . Name } Struct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>>
132+ { nestedIndentation } { {" ) ;
133+ GenFields ( lm , nestedIndentation ) ;
106134
107135 if ( lm . TemplateParameters . Count > 0 )
108136 {
109137 _builder . Append ( $@ "
110- public __{ lm . Name } Struct(" ) ;
138+ { nestedIndentation } public __{ lm . Name } Struct(" ) ;
111139 GenArguments ( lm ) ;
112140 _builder . Append ( $@ ")
113- {{" ) ;
141+ { nestedIndentation } { {" ) ;
114142 _builder . AppendLine ( ) ;
115- GenFieldAssignments ( lm ) ;
143+ GenFieldAssignments ( lm , nestedIndentation ) ;
116144 _builder . Append ( $@ "
117- }}
145+ { nestedIndentation } }}
118146" ) ;
119147 }
120148
121149 _builder . Append ( $@ "
122- public override string ToString()
123- {{
150+ { nestedIndentation } public override string ToString()
151+ { nestedIndentation } { {
124152" ) ;
125- GenVariableAssignments ( lm ) ;
153+ GenVariableAssignments ( lm , nestedIndentation ) ;
126154 _builder . Append ( $@ "
127- return $""{ lm . Message } "";
128- }}
155+ { nestedIndentation } return $""{ lm . Message } "";
156+ { nestedIndentation } }}
129157" ) ;
130158 _builder . Append ( $@ "
131- public static string Format(__{ lm . Name } Struct state, global::System.Exception? ex) => state.ToString();
159+ { nestedIndentation } public static string Format(__{ lm . Name } Struct state, global::System.Exception? ex) => state.ToString();
132160
133- public int Count => { lm . TemplateParameters . Count + 1 } ;
161+ { nestedIndentation } public int Count => { lm . TemplateParameters . Count + 1 } ;
134162
135- public global::System.Collections.Generic.KeyValuePair<string, object?> this[int index]
136- {{
137- get => index switch
138- {{
163+ { nestedIndentation } public global::System.Collections.Generic.KeyValuePair<string, object?> this[int index]
164+ { nestedIndentation } { {
165+ { nestedIndentation } get => index switch
166+ { nestedIndentation } { {
139167" ) ;
140- GenCases ( lm ) ;
168+ GenCases ( lm , nestedIndentation ) ;
141169 _builder . Append ( $@ "
142- _ => throw new global::System.IndexOutOfRangeException(nameof(index)), // return the same exception LoggerMessage.Define returns in this case
143- }};
170+ { nestedIndentation } _ => throw new global::System.IndexOutOfRangeException(nameof(index)), // return the same exception LoggerMessage.Define returns in this case
171+ { nestedIndentation } }};
144172 }}
145173
146- public global::System.Collections.Generic.IEnumerator<global::System.Collections.Generic.KeyValuePair<string, object?>> GetEnumerator()
147- {{
148- for (int i = 0; i < { lm . TemplateParameters . Count + 1 } ; i++)
149- {{
150- yield return this[i];
151- }}
152- }}
174+ { nestedIndentation } public global::System.Collections.Generic.IEnumerator<global::System.Collections.Generic.KeyValuePair<string, object?>> GetEnumerator()
175+ { nestedIndentation } { {
176+ { nestedIndentation } for (int i = 0; i < { lm . TemplateParameters . Count + 1 } ; i++)
177+ { nestedIndentation } { {
178+ { nestedIndentation } yield return this[i];
179+ { nestedIndentation } }}
180+ { nestedIndentation } }}
153181
154- global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
155- }}
182+ { nestedIndentation } global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
183+ { nestedIndentation } }}
156184" ) ;
157185 }
158186
159- private void GenFields ( LoggerMethod lm )
187+ private void GenFields ( LoggerMethod lm , string nestedIndentation )
160188 {
161189 foreach ( LoggerParameter p in lm . TemplateParameters )
162190 {
163- _builder . AppendLine ( $ " private readonly { p . Type } _{ p . Name } ;") ;
191+ _builder . AppendLine ( $ " { nestedIndentation } private readonly { p . Type } _{ p . Name } ;") ;
164192 }
165193 }
166194
167- private void GenFieldAssignments ( LoggerMethod lm )
195+ private void GenFieldAssignments ( LoggerMethod lm , string nestedIndentation )
168196 {
169197 foreach ( LoggerParameter p in lm . TemplateParameters )
170198 {
171- _builder . AppendLine ( $ " this._{ p . Name } = { p . Name } ;") ;
199+ _builder . AppendLine ( $ " { nestedIndentation } this._{ p . Name } = { p . Name } ;") ;
172200 }
173201 }
174202
175- private void GenVariableAssignments ( LoggerMethod lm )
203+ private void GenVariableAssignments ( LoggerMethod lm , string nestedIndentation )
176204 {
177205 foreach ( KeyValuePair < string , string > t in lm . TemplateMap )
178206 {
@@ -192,20 +220,20 @@ private void GenVariableAssignments(LoggerMethod lm)
192220 {
193221 if ( lm . TemplateParameters [ index ] . IsEnumerable )
194222 {
195- _builder . AppendLine ( $ " var { t . Key } = "
223+ _builder . AppendLine ( $ " { nestedIndentation } var { t . Key } = "
196224 + $ "global::__LoggerMessageGenerator.Enumerate((global::System.Collections.IEnumerable ?)this._{ lm . TemplateParameters [ index ] . Name } );") ;
197225
198226 _needEnumerationHelper = true ;
199227 }
200228 else
201229 {
202- _builder . AppendLine ( $ " var { t . Key } = this._{ lm . TemplateParameters [ index ] . Name } ;") ;
230+ _builder . AppendLine ( $ " { nestedIndentation } var { t . Key } = this._{ lm . TemplateParameters [ index ] . Name } ;") ;
203231 }
204232 }
205233 }
206234 }
207235
208- private void GenCases ( LoggerMethod lm )
236+ private void GenCases ( LoggerMethod lm , string nestedIndentation )
209237 {
210238 int index = 0 ;
211239 foreach ( LoggerParameter p in lm . TemplateParameters )
@@ -217,10 +245,10 @@ private void GenCases(LoggerMethod lm)
217245 name = lm . TemplateMap [ name ] ;
218246 }
219247
220- _builder . AppendLine ( $ " { index ++ } => new global::System.Collections.Generic.KeyValuePair<string, object?>(\" { name } \" , this._{ p . Name } ),") ;
248+ _builder . AppendLine ( $ " { nestedIndentation } { index ++ } => new global::System.Collections.Generic.KeyValuePair<string, object?>(\" { name } \" , this._{ p . Name } ),") ;
221249 }
222250
223- _builder . AppendLine ( $ " { index ++ } => new global::System.Collections.Generic.KeyValuePair<string, object?>(\" {{OriginalFormat}}\" , \" { ConvertEndOfLineAndQuotationCharactersToEscapeForm ( lm . Message ) } \" ),") ;
251+ _builder . AppendLine ( $ " { nestedIndentation } { index ++ } => new global::System.Collections.Generic.KeyValuePair<string, object?>(\" {{OriginalFormat}}\" , \" { ConvertEndOfLineAndQuotationCharactersToEscapeForm ( lm . Message ) } \" ),") ;
224252 }
225253
226254 private void GenCallbackArguments ( LoggerMethod lm )
@@ -321,7 +349,7 @@ private void GenHolder(LoggerMethod lm)
321349 _builder . Append ( ')' ) ;
322350 }
323351
324- private void GenLogMethod ( LoggerMethod lm )
352+ private void GenLogMethod ( LoggerMethod lm , string nestedIndentation )
325353 {
326354 string level = GetLogLevel ( lm ) ;
327355 string extension = lm . IsExtensionMethod ? "this " : string . Empty ;
@@ -332,13 +360,13 @@ private void GenLogMethod(LoggerMethod lm)
332360 if ( UseLoggerMessageDefine ( lm ) )
333361 {
334362 _builder . Append ( $@ "
335- [{ s_generatedCodeAttribute } ]
336- private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, " ) ;
363+ { nestedIndentation } [{ s_generatedCodeAttribute } ]
364+ { nestedIndentation } private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, " ) ;
337365
338366 GenDefineTypes ( lm , brackets : false ) ;
339367
340- _builder . Append ( @$ "global::System.Exception?> __{ lm . Name } Callback =
341- global::Microsoft.Extensions.Logging.LoggerMessage.Define" ) ;
368+ _builder . Append ( $@ "global::System.Exception?> __{ lm . Name } Callback =
369+ { nestedIndentation } global::Microsoft.Extensions.Logging.LoggerMessage.Define" ) ;
342370
343371 GenDefineTypes ( lm , brackets : true ) ;
344372
@@ -347,20 +375,20 @@ private void GenLogMethod(LoggerMethod lm)
347375 }
348376
349377 _builder . Append ( $@ "
350- [{ s_generatedCodeAttribute } ]
351- { lm . Modifiers } void { lm . Name } ({ extension } " ) ;
378+ { nestedIndentation } [{ s_generatedCodeAttribute } ]
379+ { nestedIndentation } { lm . Modifiers } void { lm . Name } ({ extension } " ) ;
352380
353381 GenParameters ( lm ) ;
354382
355383 _builder . Append ( $@ ")
356- {{
357- if ({ logger } .IsEnabled({ level } ))
358- {{" ) ;
384+ { nestedIndentation } { {
385+ { nestedIndentation } if ({ logger } .IsEnabled({ level } ))
386+ { nestedIndentation } { {" ) ;
359387
360388 if ( UseLoggerMessageDefine ( lm ) )
361389 {
362390 _builder . Append ( $@ "
363- __{ lm . Name } Callback({ logger } , " ) ;
391+ { nestedIndentation } __{ lm . Name } Callback({ logger } , " ) ;
364392
365393 GenCallbackArguments ( lm ) ;
366394
@@ -369,7 +397,7 @@ private void GenLogMethod(LoggerMethod lm)
369397 else
370398 {
371399 _builder . Append ( $@ "
372- { logger } .Log(
400+ { nestedIndentation } { logger } .Log(
373401 { level } ,
374402 new global::Microsoft.Extensions.Logging.EventId({ lm . EventId } , { eventName } ),
375403 " ) ;
@@ -380,8 +408,8 @@ private void GenLogMethod(LoggerMethod lm)
380408 }
381409
382410 _builder . Append ( $@ "
383- }}
384- }}" ) ;
411+ { nestedIndentation } }}
412+ { nestedIndentation } }}" ) ;
385413
386414 static string GetException ( LoggerMethod lm )
387415 {
0 commit comments