Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Routing Logging
Browse files Browse the repository at this point in the history
Added scoped logging to RouterMiddleware, RouteCollection, and
TemplateRoute.
  • Loading branch information
Ben Brown committed Aug 1, 2014
1 parent 61436fb commit fca9831
Show file tree
Hide file tree
Showing 28 changed files with 1,543 additions and 206 deletions.
37 changes: 37 additions & 0 deletions src/Microsoft.AspNet.Routing/Logging/LogFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Open Technologies, Inc. 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
{
public static class LogFormatter
{
/// <summary>
/// A formatter for use with <see cref="Microsoft.Framework.Logging.ILogger.WriteCore(
/// Framework.Logging.TraceType,
/// int,
/// object,
/// Exception, Func{object, Exception, string})"/>.
/// </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 "";
}
}
}
20 changes: 20 additions & 0 deletions src/Microsoft.AspNet.Routing/Logging/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Framework.Logging;

namespace Microsoft.AspNet.Routing.Logging
{
internal static class LoggerExtensions
{
public static bool WriteValues([NotNull] this ILogger logger, object values)
{
return logger.WriteCore(
eventType: TraceType.Information,
eventId: 0,
state: values,
exception: null,
formatter: LogFormatter.Formatter);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Text;

namespace Microsoft.AspNet.Routing.Logging
{
/// <summary>
/// Describes the state of
/// <see cref="Microsoft.AspNet.Routing.RouteCollection.RouteAsync(RouteContext)"/>.
/// </summary>
public class RouteCollectionRouteAsyncValues
{
/// <summary>
/// The name of the state.
/// </summary>
public string Name
{
get
{
return "RouteCollection.RouteAsync";
}
}

/// <summary>
/// The available routes.
/// </summary>
public IList<IRouter> Routes { get; set; }

/// <summary>
/// True if the request is handled.
/// </summary>
public bool Handled { get; set; }

/// <summary>
/// A summary of the data for display.
/// </summary>
public string Summary
{
get
{
var builder = new StringBuilder();
builder.AppendLine(Name);
builder.Append("\tRoutes: ");
StringBuilderHelpers.Append(builder, Routes);
builder.AppendLine();
builder.Append("\tHandled? ");
builder.Append(Handled);
return builder.ToString();
}
}

/// <inheritdoc/>
public override string ToString()
{
return Summary;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Text;

namespace Microsoft.AspNet.Routing.Logging
{
/// <summary>
/// Describes the state of <see cref="RouteConstraintMatcher.Match(
/// System.Collections.Generic.IDictionary{string, IRouteConstraint},
/// System.Collections.Generic.IDictionary{string, object},
/// Http.HttpContext,
/// IRouter,
/// RouteDirection,
/// Framework.Logging.ILogger)"/>.
/// </summary>
public class RouteConstraintMatcherMatchValues
{
/// <summary>
/// The name of the state.
/// </summary>
public string Name
{
get
{
return "RouteConstraintMatcher.Match";
}
}

/// <summary>
/// The key of the constraint.
/// </summary>
public string ConstraintKey { get; set; }

/// <summary>
/// The constraint.
/// </summary>
public IRouteConstraint Constraint { get; set; }

/// <summary>
/// True if the <see cref="Constraint"/> matched.
/// </summary>
public bool Matched { get; set; }

/// <summary>
/// A summary of the data for display.
/// </summary>
public string Summary
{
get
{
var builder = new StringBuilder();
builder.AppendLine(Name);
builder.Append("\tConstraint key: ");
builder.AppendLine(ConstraintKey);
builder.Append("\tConstraint: ");
builder.Append(Constraint);
builder.AppendLine();
builder.Append("\tMatched? ");
builder.Append(Matched);
return builder.ToString();
}
}

/// <inheritdoc/>
public override string ToString()
{
return Summary;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Text;

namespace Microsoft.AspNet.Routing.Logging
{
/// <summary>
/// Describes the state of
/// <see cref="Microsoft.AspNet.Builder.RouterMiddleware.Invoke(Http.HttpContext)"/>.
/// </summary>
public class RouterMiddlewareInvokeValues
{
/// <summary>
/// The name of the state.
/// </summary>
public string Name
{
get
{
return "RouterMiddleware.Invoke";
}
}

/// <summary>
/// True if the request is handled.
/// </summary>
public bool Handled { get; set; }

/// <summary>
/// A summary of the data for display.
/// </summary>
public string Summary
{
get
{
var builder = new StringBuilder();
builder.AppendLine(Name);
builder.Append("\tHandled? ");
builder.Append(Handled);
return builder.ToString();
}
}

/// <inheritdoc />
public override string ToString()
{
return Summary;
}
}
}
44 changes: 44 additions & 0 deletions src/Microsoft.AspNet.Routing/Logging/StringBuilderHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Open Technologies, Inc. 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");
}
}
}
}
Loading

0 comments on commit fca9831

Please sign in to comment.