Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 3ea44c6

Browse files
committed
React to Logging API changes
1 parent e62ceb8 commit 3ea44c6

File tree

5 files changed

+123
-48
lines changed

5 files changed

+123
-48
lines changed

src/Microsoft.AspNetCore.Hosting/Internal/HostingLoggerExtensions.cs

+119-44
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Diagnostics;
5+
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using Microsoft.AspNetCore.Http;
89
using Microsoft.Extensions.Logging;
910

@@ -52,7 +53,7 @@ public static void ApplicationError(this ILogger logger, Exception exception)
5253
logger.LogError(
5354
eventId: LoggerEventIds.ApplicationStartupException,
5455
message: "Application startup exception",
55-
error: exception);
56+
exception: exception);
5657
}
5758

5859
public static void Starting(this ILogger logger)
@@ -61,7 +62,7 @@ public static void Starting(this ILogger logger)
6162
{
6263
logger.LogDebug(
6364
eventId: LoggerEventIds.Starting,
64-
data: "Hosting starting");
65+
message: "Hosting starting");
6566
}
6667
}
6768

@@ -71,7 +72,7 @@ public static void Started(this ILogger logger)
7172
{
7273
logger.LogDebug(
7374
eventId: LoggerEventIds.Started,
74-
data: "Hosting started");
75+
message: "Hosting started");
7576
}
7677
}
7778

@@ -81,17 +82,40 @@ public static void Shutdown(this ILogger logger)
8182
{
8283
logger.LogDebug(
8384
eventId: LoggerEventIds.Shutdown,
84-
data: "Hosting shutdown");
85+
message: "Hosting shutdown");
8586
}
8687
}
8788

8889

89-
private class HostingLogScope : ILogValues
90+
private class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>>
9091
{
9192
private readonly HttpContext _httpContext;
9293

9394
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+
}
95119

96120
public HostingLogScope(HttpContext httpContext)
97121
{
@@ -108,29 +132,65 @@ public override string ToString()
108132
return _cachedToString;
109133
}
110134

111-
public IEnumerable<KeyValuePair<string, object>> GetValues()
135+
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
112136
{
113-
if (_cachedGetValues == null)
137+
for (int i = 0; i < Count; ++i)
114138
{
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];
120140
}
141+
}
121142

122-
return _cachedGetValues;
143+
IEnumerator IEnumerable.GetEnumerator()
144+
{
145+
return GetEnumerator();
123146
}
124147
}
125148

126-
private class HostingRequestStarting : ILogValues
149+
private class HostingRequestStarting : IReadOnlyList<KeyValuePair<string, object>>
127150
{
128151
internal static readonly Func<object, Exception, string> Callback = (state, exception) => ((HostingRequestStarting)state).ToString();
129152

130153
private readonly HttpRequest _request;
131154

132155
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+
}
134194

135195
public HostingRequestStarting(HttpContext httpContext)
136196
{
@@ -147,38 +207,55 @@ public override string ToString()
147207
return _cachedToString;
148208
}
149209

150-
public IEnumerable<KeyValuePair<string, object>> GetValues()
210+
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
151211
{
152-
if (_cachedGetValues == null)
212+
for (int i = 0; i < Count; ++i)
153213
{
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];
166215
}
216+
}
167217

168-
return _cachedGetValues;
218+
IEnumerator IEnumerable.GetEnumerator()
219+
{
220+
return GetEnumerator();
169221
}
170222
}
171223

172-
private class HostingRequestFinished
224+
private class HostingRequestFinished : IReadOnlyList<KeyValuePair<string, object>>
173225
{
174226
internal static readonly Func<object, Exception, string> Callback = (state, exception) => ((HostingRequestFinished)state).ToString();
175227

176228
private readonly HttpContext _httpContext;
177229
private readonly TimeSpan _elapsed;
178-
179-
private IEnumerable<KeyValuePair<string, object>> _cachedGetValues;
230+
180231
private string _cachedToString;
181232

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+
182259
public HostingRequestFinished(HttpContext httpContext, TimeSpan elapsed)
183260
{
184261
_httpContext = httpContext;
@@ -195,19 +272,17 @@ public override string ToString()
195272
return _cachedToString;
196273
}
197274

198-
public IEnumerable<KeyValuePair<string, object>> GetValues()
275+
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
199276
{
200-
if (_cachedGetValues == null)
277+
for (int i = 0; i < Count; ++i)
201278
{
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];
208280
}
281+
}
209282

210-
return _cachedGetValues;
283+
IEnumerator IEnumerable.GetEnumerator()
284+
{
285+
return GetEnumerator();
211286
}
212287
}
213288
}

src/Microsoft.AspNetCore.Server.Testing/Common/RetryHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static async Task<HttpResponseMessage> RetryRequest(
5151
{
5252
if (retry == retryCount - 1)
5353
{
54-
logger.LogError("Failed to connect, retry limit exceeded.", exception);
54+
logger.LogError(0, exception, "Failed to connect, retry limit exceeded.");
5555
throw;
5656
}
5757
else

test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ public IDisposable BeginScopeImpl(object state)
611611
var stringified = state.ToString();
612612
return this;
613613
}
614-
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
614+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
615615
{
616616
var stringified = formatter(state, exception);
617617
}

test/Microsoft.AspNetCore.TestHost.Tests/ClientHandlerTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private class VerifierLogger : ILogger<WebHost>
308308
public bool IsEnabled(LogLevel logLevel) => true;
309309

310310
// This call verifies that fields of HttpRequest are accessed and valid
311-
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter) => formatter(state, exception);
311+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) => formatter(state, exception);
312312

313313
class NoopDispoasble : IDisposable
314314
{

test/Microsoft.AspNetCore.TestHost.Tests/TestClientTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private class VerifierLogger : ILogger<WebHost>
204204
public bool IsEnabled(LogLevel logLevel) => true;
205205

206206
// This call verifies that fields of HttpRequest are accessed and valid
207-
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter) => formatter(state, exception);
207+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) => formatter(state, exception);
208208

209209
class NoopDispoasble : IDisposable
210210
{

0 commit comments

Comments
 (0)