Improve logging behavior for actions scope
This commit is contained in:
parent
dcec94f6d9
commit
f2af02b1cb
|
|
@ -8,7 +8,7 @@ using Microsoft.AspNet.Mvc.Abstractions;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Controllers
|
namespace Microsoft.AspNet.Mvc.Controllers
|
||||||
{
|
{
|
||||||
[DebuggerDisplay("CA {DisplayName}(RC-{RouteConstraints.Count})")]
|
[DebuggerDisplay("{DisplayName}")]
|
||||||
public class ControllerActionDescriptor : ActionDescriptor
|
public class ControllerActionDescriptor : ActionDescriptor
|
||||||
{
|
{
|
||||||
public string ControllerName { get; set; }
|
public string ControllerName { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Mvc.Abstractions;
|
using Microsoft.AspNet.Mvc.Abstractions;
|
||||||
using Microsoft.AspNet.Mvc.Core;
|
using Microsoft.AspNet.Mvc.Core;
|
||||||
using Microsoft.AspNet.Mvc.Internal;
|
using Microsoft.AspNet.Mvc.Internal;
|
||||||
|
using Microsoft.AspNet.Mvc.Logging;
|
||||||
using Microsoft.AspNet.Mvc.Routing;
|
using Microsoft.AspNet.Mvc.Routing;
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
@ -91,7 +92,7 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
||||||
new { actionDescriptor, httpContext = context.HttpContext, routeData = context.RouteData });
|
new { actionDescriptor, httpContext = context.HttpContext, routeData = context.RouteData });
|
||||||
}
|
}
|
||||||
|
|
||||||
using (_logger.BeginScope("ActionId: {ActionId}", actionDescriptor.Id))
|
using (_logger.ActionScope(actionDescriptor))
|
||||||
{
|
{
|
||||||
_logger.LogVerbose("Executing action {ActionDisplayName}", actionDescriptor.DisplayName);
|
_logger.LogVerbose("Executing action {ActionDisplayName}", actionDescriptor.DisplayName);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.AspNet.Mvc.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.Logging
|
||||||
|
{
|
||||||
|
internal static class MvcRouteHandlerLoggerExtensions
|
||||||
|
{
|
||||||
|
public static IDisposable ActionScope(this ILogger logger, ActionDescriptor action)
|
||||||
|
{
|
||||||
|
return logger.BeginScopeImpl(new ActionLogScope(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ActionLogScope : ILogValues
|
||||||
|
{
|
||||||
|
private readonly ActionDescriptor _action;
|
||||||
|
|
||||||
|
public ActionLogScope(ActionDescriptor action)
|
||||||
|
{
|
||||||
|
if (action == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
_action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<KeyValuePair<string, object>> GetValues()
|
||||||
|
{
|
||||||
|
return new KeyValuePair<string, object>[]
|
||||||
|
{
|
||||||
|
new KeyValuePair<string, object>("ActionId", _action.Id),
|
||||||
|
new KeyValuePair<string, object>("ActionName", _action.DisplayName),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
// We don't include the _action.Id here because it's just an opaque guid, and if
|
||||||
|
// you have text logging, you can already use the requestId for correlation.
|
||||||
|
return _action.DisplayName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Single(sink.Scopes);
|
Assert.Single(sink.Scopes);
|
||||||
Assert.StartsWith("ActionId: ", sink.Scopes[0].Scope?.ToString());
|
Assert.Equal(displayName, sink.Scopes[0].Scope?.ToString());
|
||||||
Assert.Single(sink.Writes);
|
Assert.Single(sink.Writes);
|
||||||
Assert.Equal(expectedMessage, sink.Writes[0].State?.ToString());
|
Assert.Equal(expectedMessage, sink.Writes[0].State?.ToString());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue