From 6e8b2ecd632315c56e94bc60906d796a25dce256 Mon Sep 17 00:00:00 2001 From: ryanbrandenburg Date: Wed, 6 Jan 2016 11:08:07 -0800 Subject: [PATCH] * Switch to new logging style --- .../Logging/LogFormatter.cs | 33 -------------- .../Logging/LoggerExtensions.cs | 26 ----------- .../RouteConstraintMatcherExtensions.cs | 31 +++++++++++++ .../RouterMiddlewareLoggerExtensions.cs | 26 +++++++++++ .../Logging/StringBuilderHelpers.cs | 44 ------------------- src/Microsoft.AspNet.Routing/RouteBase.cs | 9 ++-- .../RouteConstraintMatcher.cs | 8 +--- .../RouterMiddleware.cs | 4 +- 8 files changed, 64 insertions(+), 117 deletions(-) delete mode 100644 src/Microsoft.AspNet.Routing/Logging/LogFormatter.cs delete mode 100644 src/Microsoft.AspNet.Routing/Logging/LoggerExtensions.cs create mode 100644 src/Microsoft.AspNet.Routing/Logging/RouteConstraintMatcherExtensions.cs create mode 100644 src/Microsoft.AspNet.Routing/Logging/RouterMiddlewareLoggerExtensions.cs delete mode 100644 src/Microsoft.AspNet.Routing/Logging/StringBuilderHelpers.cs diff --git a/src/Microsoft.AspNet.Routing/Logging/LogFormatter.cs b/src/Microsoft.AspNet.Routing/Logging/LogFormatter.cs deleted file mode 100644 index 960ef5d6d2..0000000000 --- a/src/Microsoft.AspNet.Routing/Logging/LogFormatter.cs +++ /dev/null @@ -1,33 +0,0 @@ -// 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; - -namespace Microsoft.AspNet.Routing.Logging.Internal -{ - public static class LogFormatter - { - /// - /// A formatter for use with . - /// - public static string Formatter(object o, Exception e) - { - if (o != null && e != null) - { - return o + Environment.NewLine + e; - } - - if (o != null) - { - return o.ToString(); - } - - if (e != null) - { - return e.ToString(); - } - - return ""; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Routing/Logging/LoggerExtensions.cs b/src/Microsoft.AspNet.Routing/Logging/LoggerExtensions.cs deleted file mode 100644 index 88ba7efddc..0000000000 --- a/src/Microsoft.AspNet.Routing/Logging/LoggerExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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 Microsoft.Extensions.Logging; - -namespace Microsoft.AspNet.Routing.Logging.Internal -{ - public static class LoggerExtensions - { - public static void WriteValues(this ILogger logger, object values) - { - if (logger == null) - { - throw new ArgumentNullException(nameof(logger)); - } - - logger.Log( - logLevel: LogLevel.Debug, - eventId: 0, - state: values, - exception: null, - formatter: LogFormatter.Formatter); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Routing/Logging/RouteConstraintMatcherExtensions.cs b/src/Microsoft.AspNet.Routing/Logging/RouteConstraintMatcherExtensions.cs new file mode 100644 index 0000000000..05fdb512f3 --- /dev/null +++ b/src/Microsoft.AspNet.Routing/Logging/RouteConstraintMatcherExtensions.cs @@ -0,0 +1,31 @@ +// 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 Microsoft.Extensions.Logging; + +namespace Microsoft.AspNet.Routing.Logging +{ + internal static class RouteConstraintMatcherExtensions + { + private static readonly Action _routeValueDoesNotMatchConstraint; + + static RouteConstraintMatcherExtensions() + { + _routeValueDoesNotMatchConstraint = LoggerMessage.Define( + LogLevel.Debug, + 1, + "Route value '{RouteValue}' with key '{RouteKey}' did not match " + + "the constraint '{RouteConstraint}'."); + } + + public static void RouteValueDoesNotMatchConstraint( + this ILogger logger, + object routeValue, + string routeKey, + IRouteConstraint routeConstraint) + { + _routeValueDoesNotMatchConstraint(logger, routeValue, routeKey, routeConstraint, null); + } + } +} diff --git a/src/Microsoft.AspNet.Routing/Logging/RouterMiddlewareLoggerExtensions.cs b/src/Microsoft.AspNet.Routing/Logging/RouterMiddlewareLoggerExtensions.cs new file mode 100644 index 0000000000..9c4da61543 --- /dev/null +++ b/src/Microsoft.AspNet.Routing/Logging/RouterMiddlewareLoggerExtensions.cs @@ -0,0 +1,26 @@ +// 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 Microsoft.Extensions.Logging; + +namespace Microsoft.AspNet.Routing.Logging +{ + internal static class RouterMiddlewareLoggerExtensions + { + private static readonly Action _requestDidNotMatchRoutes; + + static RouterMiddlewareLoggerExtensions() + { + _requestDidNotMatchRoutes = LoggerMessage.Define( + LogLevel.Debug, + 1, + "Request did not match any routes."); + } + + public static void RequestDidNotMatchRoutes(this ILogger logger) + { + _requestDidNotMatchRoutes(logger, null); + } + } +} diff --git a/src/Microsoft.AspNet.Routing/Logging/StringBuilderHelpers.cs b/src/Microsoft.AspNet.Routing/Logging/StringBuilderHelpers.cs deleted file mode 100644 index 16babd39bc..0000000000 --- a/src/Microsoft.AspNet.Routing/Logging/StringBuilderHelpers.cs +++ /dev/null @@ -1,44 +0,0 @@ -// 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 System.Text; - -namespace Microsoft.AspNet.Routing.Logging -{ - internal static class StringBuilderHelpers - { - public static void Append(StringBuilder builder, IEnumerable items) - { - if (items == null) - { - return; - } - - foreach (var item in items) - { - builder.Append(Environment.NewLine); - builder.Append("\t\t"); - builder.Append(item != null ? item.ToString() : "null"); - } - } - - public static void Append(StringBuilder builder, IDictionary dict) - { - if (dict == null) - { - return; - } - - foreach (var kvp in dict) - { - builder.Append(Environment.NewLine); - builder.Append("\t\t"); - builder.Append(kvp.Key != null ? kvp.Key.ToString() : "null"); - builder.Append(" : "); - builder.Append(kvp.Value != null ? kvp.Value.ToString() : "null"); - } - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Routing/RouteBase.cs b/src/Microsoft.AspNet.Routing/RouteBase.cs index c09647bd24..36bbc3349d 100644 --- a/src/Microsoft.AspNet.Routing/RouteBase.cs +++ b/src/Microsoft.AspNet.Routing/RouteBase.cs @@ -7,6 +7,7 @@ using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Routing.Internal; +using Microsoft.AspNet.Routing.Logging; using Microsoft.AspNet.Routing.Template; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -105,13 +106,9 @@ namespace Microsoft.AspNet.Routing { return TaskCache.CompletedTask; } + _logger.MatchedRouteName(Name, ParsedTemplate.TemplateText); - _logger.LogDebug( - "Request successfully matched the route with name '{RouteName}' and template '{RouteTemplate}'.", - Name, - ParsedTemplate.TemplateText); - - return OnRouteMatched(context); + return OnRouteMatched(context); } /// diff --git a/src/Microsoft.AspNet.Routing/RouteConstraintMatcher.cs b/src/Microsoft.AspNet.Routing/RouteConstraintMatcher.cs index ee4aace33d..a935ae7396 100644 --- a/src/Microsoft.AspNet.Routing/RouteConstraintMatcher.cs +++ b/src/Microsoft.AspNet.Routing/RouteConstraintMatcher.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNet.Http; +using Microsoft.AspNet.Routing.Logging; using Microsoft.Extensions.Logging; namespace Microsoft.AspNet.Routing @@ -53,12 +54,7 @@ namespace Microsoft.AspNet.Routing object routeValue; routeValues.TryGetValue(kvp.Key, out routeValue); - logger.LogDebug( - "Route value '{RouteValue}' with key '{RouteKey}' did not match " + - "the constraint '{RouteConstraint}'.", - routeValue, - kvp.Key, - kvp.Value); + logger.RouteValueDoesNotMatchConstraint(routeValue, kvp.Key, kvp.Value); } return false; diff --git a/src/Microsoft.AspNet.Routing/RouterMiddleware.cs b/src/Microsoft.AspNet.Routing/RouterMiddleware.cs index f890756fc9..2880e5a0a1 100644 --- a/src/Microsoft.AspNet.Routing/RouterMiddleware.cs +++ b/src/Microsoft.AspNet.Routing/RouterMiddleware.cs @@ -1,10 +1,10 @@ // 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.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Routing; +using Microsoft.AspNet.Routing.Logging; using Microsoft.Extensions.Logging; namespace Microsoft.AspNet.Builder @@ -35,7 +35,7 @@ namespace Microsoft.AspNet.Builder if (context.Handler == null) { - _logger.LogDebug("Request did not match any routes."); + _logger.RequestDidNotMatchRoutes(); await _next.Invoke(httpContext); } else