2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
- using System . Diagnostics ;
5
+ using System . Collections ;
6
6
using System . Collections . Generic ;
7
+ using System . Diagnostics ;
7
8
using Microsoft . AspNetCore . Http ;
8
9
using Microsoft . Extensions . Logging ;
9
10
@@ -52,7 +53,7 @@ public static void ApplicationError(this ILogger logger, Exception exception)
52
53
logger . LogError (
53
54
eventId : LoggerEventIds . ApplicationStartupException ,
54
55
message : "Application startup exception" ,
55
- error : exception ) ;
56
+ exception : exception ) ;
56
57
}
57
58
58
59
public static void Starting ( this ILogger logger )
@@ -61,7 +62,7 @@ public static void Starting(this ILogger logger)
61
62
{
62
63
logger . LogDebug (
63
64
eventId : LoggerEventIds . Starting ,
64
- data : "Hosting starting" ) ;
65
+ message : "Hosting starting" ) ;
65
66
}
66
67
}
67
68
@@ -71,7 +72,7 @@ public static void Started(this ILogger logger)
71
72
{
72
73
logger . LogDebug (
73
74
eventId : LoggerEventIds . Started ,
74
- data : "Hosting started" ) ;
75
+ message : "Hosting started" ) ;
75
76
}
76
77
}
77
78
@@ -81,17 +82,40 @@ public static void Shutdown(this ILogger logger)
81
82
{
82
83
logger . LogDebug (
83
84
eventId : LoggerEventIds . Shutdown ,
84
- data : "Hosting shutdown" ) ;
85
+ message : "Hosting shutdown" ) ;
85
86
}
86
87
}
87
88
88
89
89
- private class HostingLogScope : ILogValues
90
+ private class HostingLogScope : IReadOnlyList < KeyValuePair < string , object > >
90
91
{
91
92
private readonly HttpContext _httpContext ;
92
93
93
94
private string _cachedToString ;
94
- private IEnumerable < KeyValuePair < string , object > > _cachedGetValues ;
95
+
96
+ public int Count
97
+ {
98
+ get
99
+ {
100
+ return 2 ;
101
+ }
102
+ }
103
+
104
+ public KeyValuePair < string , object > this [ int index ]
105
+ {
106
+ get
107
+ {
108
+ if ( index == 0 )
109
+ {
110
+ return new KeyValuePair < string , object > ( "RequestId" , _httpContext . TraceIdentifier ) ;
111
+ }
112
+ else if ( index == 1 )
113
+ {
114
+ return new KeyValuePair < string , object > ( "RequestPath" , _httpContext . Request . Path . ToString ( ) ) ;
115
+ }
116
+ throw new IndexOutOfRangeException ( nameof ( index ) ) ;
117
+ }
118
+ }
95
119
96
120
public HostingLogScope ( HttpContext httpContext )
97
121
{
@@ -108,29 +132,65 @@ public override string ToString()
108
132
return _cachedToString ;
109
133
}
110
134
111
- public IEnumerable < KeyValuePair < string , object > > GetValues ( )
135
+ public IEnumerator < KeyValuePair < string , object > > GetEnumerator ( )
112
136
{
113
- if ( _cachedGetValues == null )
137
+ for ( int i = 0 ; i < Count ; ++ i )
114
138
{
115
- _cachedGetValues = new [ ]
116
- {
117
- new KeyValuePair < string , object > ( "RequestId" , _httpContext . TraceIdentifier ) ,
118
- new KeyValuePair < string , object > ( "RequestPath" , _httpContext . Request . Path . ToString ( ) ) ,
119
- } ;
139
+ yield return this [ i ] ;
120
140
}
141
+ }
121
142
122
- return _cachedGetValues ;
143
+ IEnumerator IEnumerable . GetEnumerator ( )
144
+ {
145
+ return GetEnumerator ( ) ;
123
146
}
124
147
}
125
148
126
- private class HostingRequestStarting : ILogValues
149
+ private class HostingRequestStarting : IReadOnlyList < KeyValuePair < string , object > >
127
150
{
128
151
internal static readonly Func < object , Exception , string > Callback = ( state , exception ) => ( ( HostingRequestStarting ) state ) . ToString ( ) ;
129
152
130
153
private readonly HttpRequest _request ;
131
154
132
155
private string _cachedToString ;
133
- private IEnumerable < KeyValuePair < string , object > > _cachedGetValues ;
156
+
157
+ public int Count
158
+ {
159
+ get
160
+ {
161
+ return 9 ;
162
+ }
163
+ }
164
+
165
+ public KeyValuePair < string , object > this [ int index ]
166
+ {
167
+ get
168
+ {
169
+ switch ( index )
170
+ {
171
+ case 0 :
172
+ return new KeyValuePair < string , object > ( "Protocol" , _request . Protocol ) ;
173
+ case 1 :
174
+ return new KeyValuePair < string , object > ( "Method" , _request . Method ) ;
175
+ case 2 :
176
+ return new KeyValuePair < string , object > ( "ContentType" , _request . ContentType ) ;
177
+ case 3 :
178
+ return new KeyValuePair < string , object > ( "ContentLength" , _request . ContentLength ) ;
179
+ case 4 :
180
+ return new KeyValuePair < string , object > ( "Scheme" , _request . Scheme . ToString ( ) ) ;
181
+ case 5 :
182
+ return new KeyValuePair < string , object > ( "Host" , _request . Host . ToString ( ) ) ;
183
+ case 6 :
184
+ return new KeyValuePair < string , object > ( "PathBase" , _request . PathBase . ToString ( ) ) ;
185
+ case 7 :
186
+ return new KeyValuePair < string , object > ( "Path" , _request . Path . ToString ( ) ) ;
187
+ case 8 :
188
+ return new KeyValuePair < string , object > ( "QueryString" , _request . QueryString . ToString ( ) ) ;
189
+ default :
190
+ throw new IndexOutOfRangeException ( nameof ( index ) ) ;
191
+ }
192
+ }
193
+ }
134
194
135
195
public HostingRequestStarting ( HttpContext httpContext )
136
196
{
@@ -147,38 +207,55 @@ public override string ToString()
147
207
return _cachedToString ;
148
208
}
149
209
150
- public IEnumerable < KeyValuePair < string , object > > GetValues ( )
210
+ public IEnumerator < KeyValuePair < string , object > > GetEnumerator ( )
151
211
{
152
- if ( _cachedGetValues == null )
212
+ for ( int i = 0 ; i < Count ; ++ i )
153
213
{
154
- _cachedGetValues = new [ ]
155
- {
156
- new KeyValuePair < string , object > ( "Protocol" , _request . Protocol ) ,
157
- new KeyValuePair < string , object > ( "Method" , _request . Method ) ,
158
- new KeyValuePair < string , object > ( "ContentType" , _request . ContentType ) ,
159
- new KeyValuePair < string , object > ( "ContentLength" , _request . ContentLength ) ,
160
- new KeyValuePair < string , object > ( "Scheme" , _request . Scheme . ToString ( ) ) ,
161
- new KeyValuePair < string , object > ( "Host" , _request . Host . ToString ( ) ) ,
162
- new KeyValuePair < string , object > ( "PathBase" , _request . PathBase . ToString ( ) ) ,
163
- new KeyValuePair < string , object > ( "Path" , _request . Path . ToString ( ) ) ,
164
- new KeyValuePair < string , object > ( "QueryString" , _request . QueryString . ToString ( ) ) ,
165
- } ;
214
+ yield return this [ i ] ;
166
215
}
216
+ }
167
217
168
- return _cachedGetValues ;
218
+ IEnumerator IEnumerable . GetEnumerator ( )
219
+ {
220
+ return GetEnumerator ( ) ;
169
221
}
170
222
}
171
223
172
- private class HostingRequestFinished
224
+ private class HostingRequestFinished : IReadOnlyList < KeyValuePair < string , object > >
173
225
{
174
226
internal static readonly Func < object , Exception , string > Callback = ( state , exception ) => ( ( HostingRequestFinished ) state ) . ToString ( ) ;
175
227
176
228
private readonly HttpContext _httpContext ;
177
229
private readonly TimeSpan _elapsed ;
178
-
179
- private IEnumerable < KeyValuePair < string , object > > _cachedGetValues ;
230
+
180
231
private string _cachedToString ;
181
232
233
+ public int Count
234
+ {
235
+ get
236
+ {
237
+ return 3 ;
238
+ }
239
+ }
240
+
241
+ public KeyValuePair < string , object > this [ int index ]
242
+ {
243
+ get
244
+ {
245
+ switch ( index )
246
+ {
247
+ case 0 :
248
+ return new KeyValuePair < string , object > ( "ElapsedMilliseconds" , _elapsed . TotalMilliseconds ) ;
249
+ case 1 :
250
+ return new KeyValuePair < string , object > ( "StatusCode" , _httpContext . Response . StatusCode ) ;
251
+ case 2 :
252
+ return new KeyValuePair < string , object > ( "ContentType" , _httpContext . Response . ContentType ) ;
253
+ default :
254
+ throw new IndexOutOfRangeException ( nameof ( index ) ) ;
255
+ }
256
+ }
257
+ }
258
+
182
259
public HostingRequestFinished ( HttpContext httpContext , TimeSpan elapsed )
183
260
{
184
261
_httpContext = httpContext ;
@@ -195,19 +272,17 @@ public override string ToString()
195
272
return _cachedToString ;
196
273
}
197
274
198
- public IEnumerable < KeyValuePair < string , object > > GetValues ( )
275
+ public IEnumerator < KeyValuePair < string , object > > GetEnumerator ( )
199
276
{
200
- if ( _cachedGetValues == null )
277
+ for ( int i = 0 ; i < Count ; ++ i )
201
278
{
202
- _cachedGetValues = new [ ]
203
- {
204
- new KeyValuePair < string , object > ( "ElapsedMilliseconds" , _elapsed . TotalMilliseconds ) ,
205
- new KeyValuePair < string , object > ( "StatusCode" , _httpContext . Response . StatusCode ) ,
206
- new KeyValuePair < string , object > ( "ContentType" , _httpContext . Response . ContentType ) ,
207
- } ;
279
+ yield return this [ i ] ;
208
280
}
281
+ }
209
282
210
- return _cachedGetValues ;
283
+ IEnumerator IEnumerable . GetEnumerator ( )
284
+ {
285
+ return GetEnumerator ( ) ;
211
286
}
212
287
}
213
288
}
0 commit comments