Cleanup logging in tag helpers

This commit is contained in:
Kiran Challa 2015-04-24 09:37:47 -07:00
parent f2fed5e940
commit 0a1918acac
6 changed files with 32 additions and 88 deletions

View File

@ -55,13 +55,15 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
attribute => PartiallyMatchedAttributes.Contains(
attribute, StringComparer.OrdinalIgnoreCase)));
logger.LogWarning(new PartialModeMatchLoggerStructure<TMode>(uniqueId, viewPath, partialOnlyMatches));
logger.LogWarning(new PartialModeMatchLogValues<TMode>(uniqueId, viewPath, partialOnlyMatches));
}
if (logger.IsEnabled(LogLevel.Verbose) && !FullMatches.Any())
{
logger.LogVerbose("Skipping processing for {0} {1}",
tagHelper.GetType().GetTypeInfo().FullName, uniqueId);
logger.LogVerbose(
"Skipping processing for tag helper '{TagHelper}' with id '{TagHelperId}'.",
tagHelper.GetType().GetTypeInfo().FullName,
uniqueId);
}
}
}

View File

@ -11,49 +11,50 @@ using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
{
/// <summary>
/// An <see cref="ILogValues"/> for log messages regarding <see cref="ITagHelper"/> instances that opt out of
/// Log values for <see cref="ITagHelper"/> instances that opt out of
/// processing due to missing attributes for one of several possible modes.
/// </summary>
public class PartialModeMatchLoggerStructure<TMode> : PartialModeMatchLoggerStructure
public class PartialModeMatchLogValues<TMode> : ILogValues
{
private readonly string _uniqueId;
private readonly string _viewPath;
private readonly IEnumerable<ModeMatchAttributes<TMode>> _partialMatches;
/// <summary>
/// Creates a new <see cref="PartialModeMatchLoggerStructure{TMode}"/>.
/// Creates a new <see cref="PartialModeMatchLogValues{TMode}"/>.
/// </summary>
/// <param name="uniqueId">The unique ID of the HTML element this message applies to.</param>
/// <param name="viewPath">The path to the view.</param>
/// <param name="partialMatches">The set of modes with partial required attributes.</param>
public PartialModeMatchLoggerStructure(
public PartialModeMatchLogValues(
string uniqueId,
string viewPath,
[NotNull] IEnumerable<ModeMatchAttributes<TMode>> partialMatches)
: base(values: new Dictionary<string, object>
{
["UniqueId"] = uniqueId,
["ViewPath"] = viewPath,
["PartialMatches"] = partialMatches
})
{
_uniqueId = uniqueId;
_viewPath = viewPath;
_partialMatches = partialMatches;
}
/// <summary>
/// Generates a human readable string for this structured log message.
/// </summary>
/// <returns>The message.</returns>
public override string Format()
public override string ToString()
{
var newLine = Environment.NewLine;
return string.Format(
$"Tag Helper with ID {_uniqueId} in view '{_viewPath}' had partial matches while determining mode:{newLine}\t{{0}}",
$"Tag Helper with ID '{_uniqueId}' in view '{_viewPath}' had partial matches " +
$"while determining mode:{newLine}\t{{0}}",
string.Join($"{newLine}\t", _partialMatches.Select(partial =>
string.Format($"Mode '{partial.Mode}' missing attributes:{newLine}\t\t{{0}} ",
string.Join($"{newLine}\t\t", partial.MissingAttributes)))));
}
public IEnumerable<KeyValuePair<string, object>> GetValues()
{
yield return new KeyValuePair<string, object>(
"Message",
"Tag helper had partial matches while determining mode.");
yield return new KeyValuePair<string, object>("UniqueId", _uniqueId);
yield return new KeyValuePair<string, object>("ViewPath", _viewPath);
yield return new KeyValuePair<string, object>("PartialMatches", _partialMatches);
}
}
}

View File

@ -1,48 +0,0 @@
// 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 Microsoft.AspNet.Razor.Runtime.TagHelpers;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
{
/// <summary>
/// An <see cref="ILogValues"/> for log messages regarding <see cref="ITagHelper"/> instances that opt out of
/// processing due to missing attributes for one of several possible modes.
/// </summary>
public abstract class PartialModeMatchLoggerStructure : ILogValues
{
private readonly IEnumerable<KeyValuePair<string, object>> _values;
protected PartialModeMatchLoggerStructure(IEnumerable<KeyValuePair<string, object>> values)
{
_values = values;
}
/// <summary>
/// The log message.
/// </summary>
public string Message
{
get
{
return "Tag Helper has missing required attributes.";
}
}
/// <summary>
/// Returns a human-readable string of the structured data.
/// </summary>
public abstract string Format();
/// <summary>
/// Gets the values associated with this structured log message.
/// </summary>
/// <returns>The values.</returns>
public IEnumerable<KeyValuePair<string, object>> GetValues()
{
return _values;
}
}
}

View File

@ -182,11 +182,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
[Activate]
[HtmlAttributeNotBound]
public ILoggerFactory LoggerFactory { get; set; }
// TODO: will remove LoggerFactory and activate logger once DI/hosting bug is fixed
[HtmlAttributeNotBound]
public ILogger<LinkTagHelper> Logger { get; set; }
protected internal ILogger<LinkTagHelper> Logger { get; set; }
[Activate]
[HtmlAttributeNotBound]
@ -222,9 +218,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var modeResult = AttributeMatcher.DetermineMode(context, ModeDetails);
var logger = Logger ?? LoggerFactory.CreateLogger<LinkTagHelper>();
modeResult.LogDetails(logger, this, context.UniqueId, ViewContext.View.Path);
modeResult.LogDetails(Logger, this, context.UniqueId, ViewContext.View.Path);
if (!modeResult.FullMatches.Any())
{

View File

@ -151,11 +151,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
[Activate]
[HtmlAttributeNotBound]
public ILoggerFactory LoggerFactory { get; set; }
// TODO: will remove LoggerFactory and activate logger once DI/hosting bug is fixed
[HtmlAttributeNotBound]
public ILogger<ScriptTagHelper> Logger { get; set; }
protected internal ILogger<ScriptTagHelper> Logger { get; set; }
[Activate]
[HtmlAttributeNotBound]
@ -191,9 +187,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var modeResult = AttributeMatcher.DetermineMode(context, ModeDetails);
var logger = Logger ?? LoggerFactory.CreateLogger<ScriptTagHelper>();
modeResult.LogDetails(logger, this, context.UniqueId, ViewContext.View.Path);
modeResult.LogDetails(Logger, this, context.UniqueId, ViewContext.View.Path);
if (!modeResult.FullMatches.Any())
{

View File

@ -411,13 +411,14 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
Assert.Equal(2, logger.Logged.Count);
Assert.Equal(LogLevel.Warning, logger.Logged[0].LogLevel);
Assert.IsAssignableFrom<PartialModeMatchLoggerStructure>(logger.Logged[0].State);
Assert.IsAssignableFrom<ILogValues>(logger.Logged[0].State);
var loggerData0 = (PartialModeMatchLoggerStructure)logger.Logged[0].State;
var loggerData0 = (ILogValues)logger.Logged[0].State;
Assert.Equal(LogLevel.Verbose, logger.Logged[1].LogLevel);
Assert.IsAssignableFrom<ILogValues>(logger.Logged[1].State);
Assert.StartsWith("Skipping processing for Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper",
Assert.StartsWith("Skipping processing for tag helper 'Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper'" +
" with id",
((ILogValues)logger.Logged[1].State).ToString());
}
@ -472,7 +473,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
Assert.Equal(LogLevel.Verbose, logger.Logged[0].LogLevel);
Assert.IsAssignableFrom<ILogValues>(logger.Logged[0].State);
Assert.StartsWith("Skipping processing for Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper",
Assert.StartsWith("Skipping processing for tag helper 'Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper'",
((ILogValues)logger.Logged[0].State).ToString());
}