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

Commit

Permalink
Event ids and more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Hurdugaci committed Oct 26, 2015
1 parent 2f4c344 commit 9be0758
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public virtual IApplication Start()
var logger = _applicationServices.GetRequiredService<ILogger<HostingEngine>>();
var contextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>();
var diagnosticSource = _applicationServices.GetRequiredService<DiagnosticSource>();

logger.Starting();

var server = ServerFactory.Start(_serverFeatures,
async features =>
{
Expand Down Expand Up @@ -130,9 +133,11 @@ public virtual IApplication Start()
});

_applicationLifetime.NotifyStarted();
logger.Started();

return new Application(ApplicationServices, _serverFeatures, new Disposable(() =>
{
logger.Shutdown();
_applicationLifetime.StopApplication();
server.Dispose();
_applicationLifetime.NotifyStopped();
Expand Down Expand Up @@ -218,7 +223,7 @@ private RequestDelegate BuildApplication()
// 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<ILogger<HostingEngine>>();
logger.LogError("Application startup exception", ex);
logger.ApplicationError(ex);

// Generate an HTML error page.
var runtimeEnv = _applicationServices.GetRequiredService<IRuntimeEnvironment>();
Expand Down
44 changes: 41 additions & 3 deletions src/Microsoft.AspNet.Hosting/Internal/HostingLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void RequestStarting(this ILogger logger, HttpContext httpContext)
{
logger.Log(
logLevel: LogLevel.Information,
eventId: 1,
eventId: LoggerEventIds.RequestStarting,
state: new HostingRequestStarting(httpContext),
exception: null,
formatter: HostingRequestStarting.Callback);
Expand All @@ -36,7 +36,7 @@ public static void RequestFinished(this ILogger logger, HttpContext httpContext,
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);
Expand All @@ -50,13 +50,51 @@ public static void RequestFailed(this ILogger logger, HttpContext httpContext, i
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
{
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.AspNet.Hosting/Internal/LoggerEventIds.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 9be0758

Please sign in to comment.