@@ -65,29 +65,57 @@ private static bool UseLoggerMessageDefine(LoggerMethod lm)
65
65
66
66
private void GenType ( LoggerClass lc )
67
67
{
68
+ string nestedIndentation = "" ;
68
69
if ( ! string . IsNullOrWhiteSpace ( lc . Namespace ) )
69
70
{
70
71
_builder . Append ( $@ "
71
72
namespace { lc . Namespace }
72
73
{{" ) ;
73
74
}
74
75
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
+
75
94
_builder . Append ( $@ "
76
- partial class { lc . Name } { lc . Constraints }
77
- {{" ) ;
95
+ { nestedIndentation } partial { lc . Keyword } { lc . Name } { lc . Constraints }
96
+ { nestedIndentation } { {" ) ;
78
97
79
98
foreach ( LoggerMethod lm in lc . Methods )
80
99
{
81
100
if ( ! UseLoggerMessageDefine ( lm ) )
82
101
{
83
- GenStruct ( lm ) ;
102
+ GenStruct ( lm , nestedIndentation ) ;
84
103
}
85
104
86
- GenLogMethod ( lm ) ;
105
+ GenLogMethod ( lm , nestedIndentation ) ;
87
106
}
88
107
89
108
_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
+ }
91
119
92
120
if ( ! string . IsNullOrWhiteSpace ( lc . Namespace ) )
93
121
{
@@ -96,83 +124,83 @@ partial class {lc.Name} {lc.Constraints}
96
124
}
97
125
}
98
126
99
- private void GenStruct ( LoggerMethod lm )
127
+ private void GenStruct ( LoggerMethod lm , string nestedIndentation )
100
128
{
101
129
_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 ) ;
106
134
107
135
if ( lm . TemplateParameters . Count > 0 )
108
136
{
109
137
_builder . Append ( $@ "
110
- public __{ lm . Name } Struct(" ) ;
138
+ { nestedIndentation } public __{ lm . Name } Struct(" ) ;
111
139
GenArguments ( lm ) ;
112
140
_builder . Append ( $@ ")
113
- {{" ) ;
141
+ { nestedIndentation } { {" ) ;
114
142
_builder . AppendLine ( ) ;
115
- GenFieldAssignments ( lm ) ;
143
+ GenFieldAssignments ( lm , nestedIndentation ) ;
116
144
_builder . Append ( $@ "
117
- }}
145
+ { nestedIndentation } }}
118
146
" ) ;
119
147
}
120
148
121
149
_builder . Append ( $@ "
122
- public override string ToString()
123
- {{
150
+ { nestedIndentation } public override string ToString()
151
+ { nestedIndentation } { {
124
152
" ) ;
125
- GenVariableAssignments ( lm ) ;
153
+ GenVariableAssignments ( lm , nestedIndentation ) ;
126
154
_builder . Append ( $@ "
127
- return $""{ lm . Message } "";
128
- }}
155
+ { nestedIndentation } return $""{ lm . Message } "";
156
+ { nestedIndentation } }}
129
157
" ) ;
130
158
_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();
132
160
133
- public int Count => { lm . TemplateParameters . Count + 1 } ;
161
+ { nestedIndentation } public int Count => { lm . TemplateParameters . Count + 1 } ;
134
162
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 } { {
139
167
" ) ;
140
- GenCases ( lm ) ;
168
+ GenCases ( lm , nestedIndentation ) ;
141
169
_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 } }};
144
172
}}
145
173
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 } }}
153
181
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 } }}
156
184
" ) ;
157
185
}
158
186
159
- private void GenFields ( LoggerMethod lm )
187
+ private void GenFields ( LoggerMethod lm , string nestedIndentation )
160
188
{
161
189
foreach ( LoggerParameter p in lm . TemplateParameters )
162
190
{
163
- _builder . AppendLine ( $ " private readonly { p . Type } _{ p . Name } ;") ;
191
+ _builder . AppendLine ( $ " { nestedIndentation } private readonly { p . Type } _{ p . Name } ;") ;
164
192
}
165
193
}
166
194
167
- private void GenFieldAssignments ( LoggerMethod lm )
195
+ private void GenFieldAssignments ( LoggerMethod lm , string nestedIndentation )
168
196
{
169
197
foreach ( LoggerParameter p in lm . TemplateParameters )
170
198
{
171
- _builder . AppendLine ( $ " this._{ p . Name } = { p . Name } ;") ;
199
+ _builder . AppendLine ( $ " { nestedIndentation } this._{ p . Name } = { p . Name } ;") ;
172
200
}
173
201
}
174
202
175
- private void GenVariableAssignments ( LoggerMethod lm )
203
+ private void GenVariableAssignments ( LoggerMethod lm , string nestedIndentation )
176
204
{
177
205
foreach ( KeyValuePair < string , string > t in lm . TemplateMap )
178
206
{
@@ -192,20 +220,20 @@ private void GenVariableAssignments(LoggerMethod lm)
192
220
{
193
221
if ( lm . TemplateParameters [ index ] . IsEnumerable )
194
222
{
195
- _builder . AppendLine ( $ " var { t . Key } = "
223
+ _builder . AppendLine ( $ " { nestedIndentation } var { t . Key } = "
196
224
+ $ "global::__LoggerMessageGenerator.Enumerate((global::System.Collections.IEnumerable ?)this._{ lm . TemplateParameters [ index ] . Name } );") ;
197
225
198
226
_needEnumerationHelper = true ;
199
227
}
200
228
else
201
229
{
202
- _builder . AppendLine ( $ " var { t . Key } = this._{ lm . TemplateParameters [ index ] . Name } ;") ;
230
+ _builder . AppendLine ( $ " { nestedIndentation } var { t . Key } = this._{ lm . TemplateParameters [ index ] . Name } ;") ;
203
231
}
204
232
}
205
233
}
206
234
}
207
235
208
- private void GenCases ( LoggerMethod lm )
236
+ private void GenCases ( LoggerMethod lm , string nestedIndentation )
209
237
{
210
238
int index = 0 ;
211
239
foreach ( LoggerParameter p in lm . TemplateParameters )
@@ -217,10 +245,10 @@ private void GenCases(LoggerMethod lm)
217
245
name = lm . TemplateMap [ name ] ;
218
246
}
219
247
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 } ),") ;
221
249
}
222
250
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 ) } \" ),") ;
224
252
}
225
253
226
254
private void GenCallbackArguments ( LoggerMethod lm )
@@ -321,7 +349,7 @@ private void GenHolder(LoggerMethod lm)
321
349
_builder . Append ( ')' ) ;
322
350
}
323
351
324
- private void GenLogMethod ( LoggerMethod lm )
352
+ private void GenLogMethod ( LoggerMethod lm , string nestedIndentation )
325
353
{
326
354
string level = GetLogLevel ( lm ) ;
327
355
string extension = lm . IsExtensionMethod ? "this " : string . Empty ;
@@ -332,13 +360,13 @@ private void GenLogMethod(LoggerMethod lm)
332
360
if ( UseLoggerMessageDefine ( lm ) )
333
361
{
334
362
_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, " ) ;
337
365
338
366
GenDefineTypes ( lm , brackets : false ) ;
339
367
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" ) ;
342
370
343
371
GenDefineTypes ( lm , brackets : true ) ;
344
372
@@ -347,20 +375,20 @@ private void GenLogMethod(LoggerMethod lm)
347
375
}
348
376
349
377
_builder . Append ( $@ "
350
- [{ s_generatedCodeAttribute } ]
351
- { lm . Modifiers } void { lm . Name } ({ extension } " ) ;
378
+ { nestedIndentation } [{ s_generatedCodeAttribute } ]
379
+ { nestedIndentation } { lm . Modifiers } void { lm . Name } ({ extension } " ) ;
352
380
353
381
GenParameters ( lm ) ;
354
382
355
383
_builder . Append ( $@ ")
356
- {{
357
- if ({ logger } .IsEnabled({ level } ))
358
- {{" ) ;
384
+ { nestedIndentation } { {
385
+ { nestedIndentation } if ({ logger } .IsEnabled({ level } ))
386
+ { nestedIndentation } { {" ) ;
359
387
360
388
if ( UseLoggerMessageDefine ( lm ) )
361
389
{
362
390
_builder . Append ( $@ "
363
- __{ lm . Name } Callback({ logger } , " ) ;
391
+ { nestedIndentation } __{ lm . Name } Callback({ logger } , " ) ;
364
392
365
393
GenCallbackArguments ( lm ) ;
366
394
@@ -369,7 +397,7 @@ private void GenLogMethod(LoggerMethod lm)
369
397
else
370
398
{
371
399
_builder . Append ( $@ "
372
- { logger } .Log(
400
+ { nestedIndentation } { logger } .Log(
373
401
{ level } ,
374
402
new global::Microsoft.Extensions.Logging.EventId({ lm . EventId } , { eventName } ),
375
403
" ) ;
@@ -380,8 +408,8 @@ private void GenLogMethod(LoggerMethod lm)
380
408
}
381
409
382
410
_builder . Append ( $@ "
383
- }}
384
- }}" ) ;
411
+ { nestedIndentation } }}
412
+ { nestedIndentation } }}" ) ;
385
413
386
414
static string GetException ( LoggerMethod lm )
387
415
{
0 commit comments