@@ -29,19 +29,35 @@ public static void RequestStarting(this ILogger logger, HttpContext httpContext)
29
29
}
30
30
}
31
31
32
- public static void RequestFinished ( this ILogger logger , HttpContext httpContext )
32
+ public static void RequestFinished ( this ILogger logger , HttpContext httpContext , int startTimeInTicks )
33
33
{
34
34
if ( logger . IsEnabled ( LogLevel . Information ) )
35
35
{
36
+ var elapsed = new TimeSpan ( Environment . TickCount - startTimeInTicks ) ;
36
37
logger . Log (
37
38
logLevel : LogLevel . Information ,
38
39
eventId : 2 ,
39
- state : new HostingRequestFinished ( httpContext ) ,
40
+ state : new HostingRequestFinished ( httpContext , elapsed ) ,
40
41
exception : null ,
41
42
formatter : HostingRequestFinished . Callback ) ;
42
43
}
43
44
}
44
45
46
+ public static void RequestFailed ( this ILogger logger , HttpContext httpContext , int startTimeInTicks )
47
+ {
48
+ if ( logger . IsEnabled ( LogLevel . Information ) )
49
+ {
50
+ var elapsed = new TimeSpan ( Environment . TickCount - startTimeInTicks ) ;
51
+ logger . Log (
52
+ logLevel : LogLevel . Information ,
53
+ eventId : 2 ,
54
+ state : new HostingRequestFailed ( httpContext , elapsed ) ,
55
+ exception : null ,
56
+ formatter : HostingRequestFailed . Callback ) ;
57
+ }
58
+ }
59
+
60
+
45
61
private class HostingLogScope : ILogValues
46
62
{
47
63
private readonly HttpContext _httpContext ;
@@ -110,29 +126,64 @@ private class HostingRequestFinished
110
126
internal static readonly Func < object , Exception , string > Callback = ( state , exception ) => ( ( HostingRequestFinished ) state ) . ToString ( ) ;
111
127
112
128
private readonly HttpContext _httpContext ;
129
+ private readonly TimeSpan _elapsed ;
113
130
114
131
private IEnumerable < KeyValuePair < string , object > > _cachedGetValues ;
115
132
private string _cachedToString ;
116
133
117
- public HostingRequestFinished ( HttpContext httpContext )
134
+ public HostingRequestFinished ( HttpContext httpContext , TimeSpan elapsed )
118
135
{
119
136
_httpContext = httpContext ;
137
+ _elapsed = elapsed ;
120
138
}
121
139
122
140
public override string ToString ( ) => _cachedToString ?? Interlocked . CompareExchange (
123
141
ref _cachedToString ,
124
- $ "Request finished { _httpContext . Response . StatusCode } { _httpContext . Response . ContentType } ",
142
+ $ "Request finished in { _elapsed . TotalMilliseconds } ms { _httpContext . Response . StatusCode } { _httpContext . Response . ContentType } ",
125
143
null ) ;
126
144
127
145
public IEnumerable < KeyValuePair < string , object > > GetValues ( ) => _cachedGetValues ?? Interlocked . CompareExchange (
128
146
ref _cachedGetValues ,
129
147
new [ ]
130
148
{
149
+ new KeyValuePair < string , object > ( "ElapsedMilliseconds" , _elapsed . TotalMilliseconds ) ,
131
150
new KeyValuePair < string , object > ( "StatusCode" , _httpContext . Response . StatusCode ) ,
132
151
new KeyValuePair < string , object > ( "ContentType" , _httpContext . Response . ContentType ) ,
133
152
} ,
134
153
null ) ;
135
154
}
155
+
156
+ private class HostingRequestFailed
157
+ {
158
+ internal static readonly Func < object , Exception , string > Callback = ( state , exception ) => ( ( HostingRequestFailed ) state ) . ToString ( ) ;
159
+
160
+ private readonly HttpContext _httpContext ;
161
+ private readonly TimeSpan _elapsed ;
162
+
163
+ private IEnumerable < KeyValuePair < string , object > > _cachedGetValues ;
164
+ private string _cachedToString ;
165
+
166
+ public HostingRequestFailed ( HttpContext httpContext , TimeSpan elapsed )
167
+ {
168
+ _httpContext = httpContext ;
169
+ _elapsed = elapsed ;
170
+ }
171
+
172
+ public override string ToString ( ) => _cachedToString ?? Interlocked . CompareExchange (
173
+ ref _cachedToString ,
174
+ $ "Request finished in { _elapsed . TotalMilliseconds } ms 500",
175
+ null ) ;
176
+
177
+ public IEnumerable < KeyValuePair < string , object > > GetValues ( ) => _cachedGetValues ?? Interlocked . CompareExchange (
178
+ ref _cachedGetValues ,
179
+ new [ ]
180
+ {
181
+ new KeyValuePair < string , object > ( "ElapsedMilliseconds" , _elapsed . TotalMilliseconds ) ,
182
+ new KeyValuePair < string , object > ( "StatusCode" , 500 ) ,
183
+ new KeyValuePair < string , object > ( "ContentType" , null ) ,
184
+ } ,
185
+ null ) ;
186
+ }
136
187
}
137
188
}
138
189
0 commit comments