* Switch to new logging style

This commit is contained in:
ryanbrandenburg 2016-01-06 11:08:07 -08:00
parent 739dc7d621
commit 6e8b2ecd63
8 changed files with 64 additions and 117 deletions

View File

@ -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
{
/// <summary>
/// A formatter for use with <see cref="Microsoft.Extensions.Logging.ILogger.Log"/>.
/// </summary>
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 "";
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<ILogger, object, string, IRouteConstraint, Exception> _routeValueDoesNotMatchConstraint;
static RouteConstraintMatcherExtensions()
{
_routeValueDoesNotMatchConstraint = LoggerMessage.Define<object, string, IRouteConstraint>(
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);
}
}
}

View File

@ -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<ILogger, Exception> _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);
}
}
}

View File

@ -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<T>(StringBuilder builder, IEnumerable<T> 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<K, V>(StringBuilder builder, IDictionary<K, V> 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");
}
}
}
}

View File

@ -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);
}
/// <inheritdoc />

View File

@ -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;

View File

@ -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