Skip to content

Commit 48d731b

Browse files
committed
Changes per PR feedback
1 parent 01da183 commit 48d731b

13 files changed

+158
-177
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostedService.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Linq;
8+
using System.Reflection;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.AspNetCore.Hosting.Builder;
@@ -18,18 +19,19 @@
1819

1920
namespace Microsoft.AspNetCore.Hosting
2021
{
21-
internal class GenericWebHostService : IHostedService
22+
internal sealed partial class GenericWebHostService : IHostedService
2223
{
23-
public GenericWebHostService(IOptions<GenericWebHostServiceOptions> options,
24-
IServer server,
25-
ILoggerFactory loggerFactory,
26-
DiagnosticListener diagnosticListener,
27-
ActivitySource activitySource,
28-
IHttpContextFactory httpContextFactory,
29-
IApplicationBuilderFactory applicationBuilderFactory,
30-
IEnumerable<IStartupFilter> startupFilters,
31-
IConfiguration configuration,
32-
IWebHostEnvironment hostingEnvironment)
24+
public GenericWebHostService(
25+
IOptions<GenericWebHostServiceOptions> options,
26+
IServer server,
27+
ILoggerFactory loggerFactory,
28+
DiagnosticListener diagnosticListener,
29+
ActivitySource activitySource,
30+
IHttpContextFactory httpContextFactory,
31+
IApplicationBuilderFactory applicationBuilderFactory,
32+
IEnumerable<IStartupFilter> startupFilters,
33+
IConfiguration configuration,
34+
IWebHostEnvironment hostingEnvironment)
3335
{
3436
Options = options.Value;
3537
Server = server;
@@ -122,15 +124,15 @@ public async Task StartAsync(CancellationToken cancellationToken)
122124
{
123125
foreach (var address in addresses)
124126
{
125-
LifetimeLogger.ListeningOnAddress(address);
127+
Log.ListeningOnAddress(LifetimeLogger, address);
126128
}
127129
}
128130

129131
if (Logger.IsEnabled(LogLevel.Debug))
130132
{
131133
foreach (var assembly in Options.WebHostOptions.GetFinalHostingStartupAssemblies())
132134
{
133-
Logger.StartupAssemblyLoaded(assembly);
135+
Log.StartupAssemblyLoaded(Logger, assembly);
134136
}
135137
}
136138

@@ -154,5 +156,19 @@ public async Task StopAsync(CancellationToken cancellationToken)
154156
HostingEventSource.Log.HostStop();
155157
}
156158
}
159+
160+
private static partial class Log
161+
{
162+
[LoggerMessage(14, LogLevel.Information,
163+
"Now listening on: {address}",
164+
EventName = "ListeningOnAddress")]
165+
public static partial void ListeningOnAddress(ILogger logger, string address);
166+
167+
[LoggerMessage(13, LogLevel.Debug,
168+
"Loaded hosting startup assembly {assemblyName}",
169+
EventName = "HostingStartupAssemblyLoaded",
170+
SkipEnabledCheck = true)]
171+
public static partial void StartupAssemblyLoaded(ILogger logger, string assemblyName);
172+
}
157173
}
158174
}

src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
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.Collections;
6+
using System.Collections.Generic;
57
using System.Diagnostics;
8+
using System.Globalization;
69
using System.Runtime.CompilerServices;
710
using System.Web;
811
using Microsoft.AspNetCore.Http;
@@ -87,7 +90,7 @@ public void BeginRequest(HttpContext httpContext, HostingApplication.Context con
8790
// Scope may be relevant for a different level of logging, so we always create it
8891
// see: https://github.com/aspnet/Hosting/pull/944
8992
// Scope can be null if logging is not on.
90-
context.Scope = _logger.RequestScope(httpContext);
93+
context.Scope = Log.RequestScope(_logger, httpContext);
9194

9295
if (_logger.IsEnabled(LogLevel.Information))
9396
{
@@ -359,5 +362,75 @@ private void StopActivity(Activity activity, HttpContext httpContext)
359362
_diagnosticListener.Write(ActivityStopKey, httpContext);
360363
activity.Stop(); // Resets Activity.Current (we want this after the Write)
361364
}
365+
366+
private static class Log
367+
{
368+
public static IDisposable RequestScope(ILogger logger, HttpContext httpContext)
369+
{
370+
return logger.BeginScope(new HostingLogScope(httpContext));
371+
}
372+
373+
private sealed class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>>
374+
{
375+
private readonly string _path;
376+
private readonly string _traceIdentifier;
377+
378+
private string? _cachedToString;
379+
380+
public int Count => 2;
381+
382+
public KeyValuePair<string, object> this[int index]
383+
{
384+
get
385+
{
386+
if (index == 0)
387+
{
388+
return new KeyValuePair<string, object>("RequestId", _traceIdentifier);
389+
}
390+
else if (index == 1)
391+
{
392+
return new KeyValuePair<string, object>("RequestPath", _path);
393+
}
394+
395+
throw new ArgumentOutOfRangeException(nameof(index));
396+
}
397+
}
398+
399+
public HostingLogScope(HttpContext httpContext)
400+
{
401+
_traceIdentifier = httpContext.TraceIdentifier;
402+
_path = (httpContext.Request.PathBase.HasValue
403+
? httpContext.Request.PathBase + httpContext.Request.Path
404+
: httpContext.Request.Path).ToString();
405+
}
406+
407+
public override string ToString()
408+
{
409+
if (_cachedToString == null)
410+
{
411+
_cachedToString = string.Format(
412+
CultureInfo.InvariantCulture,
413+
"RequestPath:{0} RequestId:{1}",
414+
_path,
415+
_traceIdentifier);
416+
}
417+
418+
return _cachedToString;
419+
}
420+
421+
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
422+
{
423+
for (var i = 0; i < Count; ++i)
424+
{
425+
yield return this[i];
426+
}
427+
}
428+
429+
IEnumerator IEnumerable.GetEnumerator()
430+
{
431+
return GetEnumerator();
432+
}
433+
}
434+
}
362435
}
363436
}

src/Hosting/Hosting/src/Internal/HostingLoggerExtensions.cs

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,13 @@
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.Collections;
6-
using System.Collections.Generic;
7-
using System.Globalization;
85
using System.Reflection;
9-
using Microsoft.AspNetCore.Http;
106
using Microsoft.Extensions.Logging;
117

128
namespace Microsoft.AspNetCore.Hosting
139
{
1410
internal static partial class HostingLoggerExtensions
1511
{
16-
public static IDisposable RequestScope(this ILogger logger, HttpContext httpContext)
17-
{
18-
return logger.BeginScope(new HostingLogScope(httpContext));
19-
}
20-
21-
[LoggerMessage(LoggerEventIds.ServerListeningOnAddresses, LogLevel.Information,
22-
"Now listening on: {address}",
23-
EventName = "ListeningOnAddress")]
24-
public static partial void ListeningOnAddress(this ILogger logger, string address);
25-
26-
[LoggerMessage(LoggerEventIds.HostingStartupAssemblyLoaded, LogLevel.Debug,
27-
"Loaded hosting startup assembly {assemblyName}",
28-
EventName = "HostingStartupAssemblyLoaded",
29-
SkipEnabledCheck = true)]
30-
public static partial void StartupAssemblyLoaded(this ILogger logger, string assemblyName);
31-
3212
public static void ApplicationError(this ILogger logger, Exception exception)
3313
{
3414
logger.ApplicationError(
@@ -63,115 +43,6 @@ public static void ApplicationError(this ILogger logger, EventId eventId, string
6343
message: message,
6444
exception: exception);
6545
}
66-
67-
public static void Starting(this ILogger logger)
68-
{
69-
if (logger.IsEnabled(LogLevel.Debug))
70-
{
71-
logger.LogDebug(
72-
eventId: LoggerEventIds.Starting,
73-
message: "Hosting starting");
74-
}
75-
}
76-
77-
public static void Started(this ILogger logger)
78-
{
79-
if (logger.IsEnabled(LogLevel.Debug))
80-
{
81-
logger.LogDebug(
82-
eventId: LoggerEventIds.Started,
83-
message: "Hosting started");
84-
}
85-
}
86-
87-
public static void Shutdown(this ILogger logger)
88-
{
89-
if (logger.IsEnabled(LogLevel.Debug))
90-
{
91-
logger.LogDebug(
92-
eventId: LoggerEventIds.Shutdown,
93-
message: "Hosting shutdown");
94-
}
95-
}
96-
97-
public static void ServerShutdownException(this ILogger logger, Exception ex)
98-
{
99-
if (logger.IsEnabled(LogLevel.Debug))
100-
{
101-
logger.LogDebug(
102-
eventId: LoggerEventIds.ServerShutdownException,
103-
exception: ex,
104-
message: "Server shutdown exception");
105-
}
106-
}
107-
108-
private class HostingLogScope : IReadOnlyList<KeyValuePair<string, object>>
109-
{
110-
private readonly string _path;
111-
private readonly string _traceIdentifier;
112-
113-
private string? _cachedToString;
114-
115-
public int Count
116-
{
117-
get
118-
{
119-
return 2;
120-
}
121-
}
122-
123-
public KeyValuePair<string, object> this[int index]
124-
{
125-
get
126-
{
127-
if (index == 0)
128-
{
129-
return new KeyValuePair<string, object>("RequestId", _traceIdentifier);
130-
}
131-
else if (index == 1)
132-
{
133-
return new KeyValuePair<string, object>("RequestPath", _path);
134-
}
135-
136-
throw new ArgumentOutOfRangeException(nameof(index));
137-
}
138-
}
139-
140-
public HostingLogScope(HttpContext httpContext)
141-
{
142-
_traceIdentifier = httpContext.TraceIdentifier;
143-
_path = (httpContext.Request.PathBase.HasValue
144-
? httpContext.Request.PathBase + httpContext.Request.Path
145-
: httpContext.Request.Path).ToString();
146-
}
147-
148-
public override string ToString()
149-
{
150-
if (_cachedToString == null)
151-
{
152-
_cachedToString = string.Format(
153-
CultureInfo.InvariantCulture,
154-
"RequestPath:{0} RequestId:{1}",
155-
_path,
156-
_traceIdentifier);
157-
}
158-
159-
return _cachedToString;
160-
}
161-
162-
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
163-
{
164-
for (var i = 0; i < Count; ++i)
165-
{
166-
yield return this[i];
167-
}
168-
}
169-
170-
IEnumerator IEnumerable.GetEnumerator()
171-
{
172-
return GetEnumerator();
173-
}
174-
}
17546
}
17647
}
17748

0 commit comments

Comments
 (0)