diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index 6126d712ec..55e2cd589c 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -89,6 +89,9 @@ namespace Microsoft.AspNet.Hosting.Internal var logger = _applicationServices.GetRequiredService>(); var contextFactory = _applicationServices.GetRequiredService(); var diagnosticSource = _applicationServices.GetRequiredService(); + + logger.Starting(); + var server = ServerFactory.Start(_serverFeatures, async features => { @@ -130,9 +133,11 @@ namespace Microsoft.AspNet.Hosting.Internal }); _applicationLifetime.NotifyStarted(); + logger.Started(); return new Application(ApplicationServices, _serverFeatures, new Disposable(() => { + logger.Shutdown(); _applicationLifetime.StopApplication(); server.Dispose(); _applicationLifetime.NotifyStopped(); @@ -218,7 +223,7 @@ namespace Microsoft.AspNet.Hosting.Internal // Write errors to standard out so they can be retrieved when not in development mode. Console.Out.WriteLine("Application startup exception: " + ex.ToString()); var logger = _applicationServices.GetRequiredService>(); - logger.LogError("Application startup exception", ex); + logger.ApplicationError(ex); // Generate an HTML error page. var runtimeEnv = _applicationServices.GetRequiredService(); diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs index 3f6b109c81..0d79f7ae63 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Hosting.Internal { logger.Log( logLevel: LogLevel.Information, - eventId: 1, + eventId: LoggerEventIds.RequestStarting, state: new HostingRequestStarting(httpContext), exception: null, formatter: HostingRequestStarting.Callback); @@ -36,7 +36,7 @@ namespace Microsoft.AspNet.Hosting.Internal var elapsed = new TimeSpan(Environment.TickCount - startTimeInTicks); logger.Log( logLevel: LogLevel.Information, - eventId: 2, + eventId: LoggerEventIds.RequestFinished, state: new HostingRequestFinished(httpContext, elapsed), exception: null, formatter: HostingRequestFinished.Callback); @@ -50,13 +50,51 @@ namespace Microsoft.AspNet.Hosting.Internal var elapsed = new TimeSpan(Environment.TickCount - startTimeInTicks); logger.Log( logLevel: LogLevel.Information, - eventId: 2, + eventId: LoggerEventIds.RequestFailed, state: new HostingRequestFailed(httpContext, elapsed), exception: null, formatter: HostingRequestFailed.Callback); } } + public static void ApplicationError(this ILogger logger, Exception exception) + { + logger.LogError( + eventId: LoggerEventIds.ApplicationStartupException, + message: "Application startup exception", + error: exception); + } + + public static void Starting(this ILogger logger) + { + if (logger.IsEnabled(LogLevel.Verbose)) + { + logger.LogVerbose( + eventId: LoggerEventIds.Starting, + data: "Hosting starting"); + } + } + + public static void Started(this ILogger logger) + { + if (logger.IsEnabled(LogLevel.Verbose)) + { + logger.LogVerbose( + eventId: LoggerEventIds.Started, + data: "Hosting started"); + } + } + + public static void Shutdown(this ILogger logger) + { + if (logger.IsEnabled(LogLevel.Verbose)) + { + logger.LogVerbose( + eventId: LoggerEventIds.Shutdown, + data: "Hosting shutdown"); + } + } + private class HostingLogScope : ILogValues { diff --git a/src/Microsoft.AspNet.Hosting/Internal/LoggerEventIds.cs b/src/Microsoft.AspNet.Hosting/Internal/LoggerEventIds.cs new file mode 100644 index 0000000000..9bc4a4affe --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/Internal/LoggerEventIds.cs @@ -0,0 +1,16 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNet.Hosting.Internal +{ + internal static class LoggerEventIds + { + public const int RequestStarting = 1; + public const int RequestFinished = 2; + public const int RequestFailed = 3; + public const int Starting = 4; + public const int Started = 5; + public const int Shutdown = 6; + public const int ApplicationStartupException = 7; + } +}