From f2af02b1cb1d61df073c34ae79f14a05bbc37ba5 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Thu, 22 Oct 2015 11:10:35 -0700 Subject: [PATCH] Improve logging behavior for actions scope --- .../Controllers/ControllerActionDescriptor.cs | 2 +- .../Infrastructure/MvcRouteHandler.cs | 3 +- .../MvcRouteHandlerLoggerExtensions.cs | 49 +++++++++++++++++++ .../Infrastructure/MvcRouteHandlerTests.cs | 2 +- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Core/Logging/MvcRouteHandlerLoggerExtensions.cs diff --git a/src/Microsoft.AspNet.Mvc.Core/Controllers/ControllerActionDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/Controllers/ControllerActionDescriptor.cs index 8348b7374a..e96cdeb2f0 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Controllers/ControllerActionDescriptor.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Controllers/ControllerActionDescriptor.cs @@ -8,7 +8,7 @@ using Microsoft.AspNet.Mvc.Abstractions; namespace Microsoft.AspNet.Mvc.Controllers { - [DebuggerDisplay("CA {DisplayName}(RC-{RouteConstraints.Count})")] + [DebuggerDisplay("{DisplayName}")] public class ControllerActionDescriptor : ActionDescriptor { public string ControllerName { get; set; } diff --git a/src/Microsoft.AspNet.Mvc.Core/Infrastructure/MvcRouteHandler.cs b/src/Microsoft.AspNet.Mvc.Core/Infrastructure/MvcRouteHandler.cs index ef4dd0dd8e..c275eb370b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Infrastructure/MvcRouteHandler.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Infrastructure/MvcRouteHandler.cs @@ -8,6 +8,7 @@ using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc.Abstractions; using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.Internal; +using Microsoft.AspNet.Mvc.Logging; using Microsoft.AspNet.Mvc.Routing; using Microsoft.AspNet.Routing; using Microsoft.Extensions.DependencyInjection; @@ -91,7 +92,7 @@ namespace Microsoft.AspNet.Mvc.Infrastructure 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); diff --git a/src/Microsoft.AspNet.Mvc.Core/Logging/MvcRouteHandlerLoggerExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/Logging/MvcRouteHandlerLoggerExtensions.cs new file mode 100644 index 0000000000..35ac249513 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/Logging/MvcRouteHandlerLoggerExtensions.cs @@ -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> GetValues() + { + return new KeyValuePair[] + { + new KeyValuePair("ActionId", _action.Id), + new KeyValuePair("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; + } + } + } +} diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Infrastructure/MvcRouteHandlerTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Infrastructure/MvcRouteHandlerTests.cs index 47d04a3ecd..ec55496f83 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Infrastructure/MvcRouteHandlerTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Infrastructure/MvcRouteHandlerTests.cs @@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Mvc.Infrastructure // Assert 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.Equal(expectedMessage, sink.Writes[0].State?.ToString()); }