Correct and more accurate timestamp

As per https://github.com/aspnet/Hosting/pull/543
Resolves  #3849
This commit is contained in:
Ben Adams 2016-01-01 22:32:01 +00:00 committed by Pranav K
parent 47a3aee2c1
commit c125b4e59b
4 changed files with 34 additions and 17 deletions

View File

@ -87,7 +87,7 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
{ {
_logger.ExecutingAction(actionDescriptor); _logger.ExecutingAction(actionDescriptor);
var startTime = Environment.TickCount; var startTimestamp = _logger.IsEnabled(LogLevel.Information) ? Stopwatch.GetTimestamp() : 0;
var actionContext = new ActionContext(httpContext, routeData, actionDescriptor); var actionContext = new ActionContext(httpContext, routeData, actionDescriptor);
if (_actionContextAccessor != null) if (_actionContextAccessor != null)
@ -105,7 +105,7 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
await invoker.InvokeAsync(); await invoker.InvokeAsync();
_logger.ExecutedAction(actionDescriptor, startTime); _logger.ExecutedAction(actionDescriptor, startTimestamp);
} }
} }
finally finally

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNet.Mvc.Abstractions; using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -10,6 +11,8 @@ namespace Microsoft.AspNet.Mvc.Logging
{ {
internal static class MvcRouteHandlerLoggerExtensions internal static class MvcRouteHandlerLoggerExtensions
{ {
private static readonly double TimestampToTicks = Stopwatch.Frequency / 10000000.0;
private static readonly Action<ILogger, string, Exception> _actionExecuting; private static readonly Action<ILogger, string, Exception> _actionExecuting;
private static readonly Action<ILogger, string, double, Exception> _actionExecuted; private static readonly Action<ILogger, string, double, Exception> _actionExecuted;
@ -36,10 +39,16 @@ namespace Microsoft.AspNet.Mvc.Logging
_actionExecuting(logger, action.DisplayName, null); _actionExecuting(logger, action.DisplayName, null);
} }
public static void ExecutedAction(this ILogger logger, ActionDescriptor action, int startTicks) public static void ExecutedAction(this ILogger logger, ActionDescriptor action, long startTimestamp)
{ {
var elapsed = new TimeSpan(Environment.TickCount - startTicks); // Don't log if logging wasn't enabled at start of request as time will be wildly wrong.
_actionExecuted(logger, action.DisplayName, elapsed.TotalMilliseconds, null); if (startTimestamp != 0)
{
var currentTimestamp = Stopwatch.GetTimestamp();
var elapsed = new TimeSpan((long)(TimestampToTicks * (currentTimestamp - startTimestamp)));
_actionExecuted(logger, action.DisplayName, elapsed.TotalMilliseconds, null);
}
} }
public static void NoActionsMatched(this ILogger logger) public static void NoActionsMatched(this ILogger logger)

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNet.Mvc.ViewComponents; using Microsoft.AspNet.Mvc.ViewComponents;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -10,6 +11,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures.Logging
{ {
public static class DefaultViewComponentInvokerLoggerExtensions public static class DefaultViewComponentInvokerLoggerExtensions
{ {
private static readonly double TimestampToTicks = Stopwatch.Frequency / 10000000.0;
private static readonly string[] EmptyArguments = private static readonly string[] EmptyArguments =
#if NET451 #if NET451
new string[0]; new string[0];
@ -66,16 +68,22 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures.Logging
public static void ViewComponentExecuted( public static void ViewComponentExecuted(
this ILogger logger, this ILogger logger,
ViewComponentContext context, ViewComponentContext context,
int startTime, long startTimestamp,
object result) object result)
{ {
var elapsed = new TimeSpan(Environment.TickCount - startTime); // Don't log if logging wasn't enabled at start of request as time will be wildly wrong.
_viewComponentExecuted( if (startTimestamp != 0)
logger, {
context.ViewComponentDescriptor.DisplayName, var currentTimestamp = Stopwatch.GetTimestamp();
elapsed.TotalMilliseconds, var elapsed = new TimeSpan((long)(TimestampToTicks * (currentTimestamp - startTimestamp)));
Convert.ToString(result),
null); _viewComponentExecuted(
logger,
context.ViewComponentDescriptor.DisplayName,
elapsed.TotalMilliseconds,
Convert.ToString(result),
null);
}
} }
private class ViewComponentLogScope : ILogValues private class ViewComponentLogScope : ILogValues

View File

@ -124,11 +124,11 @@ namespace Microsoft.AspNet.Mvc.ViewComponents
_diagnosticSource.BeforeViewComponent(context, component); _diagnosticSource.BeforeViewComponent(context, component);
_logger.ViewComponentExecuting(context, arguments); _logger.ViewComponentExecuting(context, arguments);
var startTime = Environment.TickCount; var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0;
var result = await ControllerActionExecutor.ExecuteAsync(method, component, arguments); var result = await ControllerActionExecutor.ExecuteAsync(method, component, arguments);
var viewComponentResult = CoerceToViewComponentResult(result); var viewComponentResult = CoerceToViewComponentResult(result);
_logger.ViewComponentExecuted(context, startTime, viewComponentResult); _logger.ViewComponentExecuted(context, startTimestamp, viewComponentResult);
_diagnosticSource.AfterViewComponent(context, viewComponentResult, component); _diagnosticSource.AfterViewComponent(context, viewComponentResult, component);
return viewComponentResult; return viewComponentResult;
@ -149,7 +149,7 @@ namespace Microsoft.AspNet.Mvc.ViewComponents
_diagnosticSource.BeforeViewComponent(context, component); _diagnosticSource.BeforeViewComponent(context, component);
_logger.ViewComponentExecuting(context, arguments); _logger.ViewComponentExecuting(context, arguments);
var startTime = Environment.TickCount; var startTimestamp = _logger.IsEnabled(LogLevel.Debug) ? Stopwatch.GetTimestamp() : 0;
object result; object result;
try try
{ {
@ -164,7 +164,7 @@ namespace Microsoft.AspNet.Mvc.ViewComponents
} }
var viewComponentResult = CoerceToViewComponentResult(result); var viewComponentResult = CoerceToViewComponentResult(result);
_logger.ViewComponentExecuted(context, startTime, viewComponentResult); _logger.ViewComponentExecuted(context, startTimestamp, viewComponentResult);
_diagnosticSource.AfterViewComponent(context, viewComponentResult, component); _diagnosticSource.AfterViewComponent(context, viewComponentResult, component);
return viewComponentResult; return viewComponentResult;