Cleanup logging in tag helpers
This commit is contained in:
parent
f2fed5e940
commit
0a1918acac
|
|
@ -55,13 +55,15 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
|
||||||
attribute => PartiallyMatchedAttributes.Contains(
|
attribute => PartiallyMatchedAttributes.Contains(
|
||||||
attribute, StringComparer.OrdinalIgnoreCase)));
|
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())
|
if (logger.IsEnabled(LogLevel.Verbose) && !FullMatches.Any())
|
||||||
{
|
{
|
||||||
logger.LogVerbose("Skipping processing for {0} {1}",
|
logger.LogVerbose(
|
||||||
tagHelper.GetType().GetTypeInfo().FullName, uniqueId);
|
"Skipping processing for tag helper '{TagHelper}' with id '{TagHelperId}'.",
|
||||||
|
tagHelper.GetType().GetTypeInfo().FullName,
|
||||||
|
uniqueId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,49 +11,50 @@ using Microsoft.Framework.Logging;
|
||||||
namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
|
namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <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.
|
/// processing due to missing attributes for one of several possible modes.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PartialModeMatchLoggerStructure<TMode> : PartialModeMatchLoggerStructure
|
public class PartialModeMatchLogValues<TMode> : ILogValues
|
||||||
{
|
{
|
||||||
private readonly string _uniqueId;
|
private readonly string _uniqueId;
|
||||||
private readonly string _viewPath;
|
private readonly string _viewPath;
|
||||||
private readonly IEnumerable<ModeMatchAttributes<TMode>> _partialMatches;
|
private readonly IEnumerable<ModeMatchAttributes<TMode>> _partialMatches;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new <see cref="PartialModeMatchLoggerStructure{TMode}"/>.
|
/// Creates a new <see cref="PartialModeMatchLogValues{TMode}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="uniqueId">The unique ID of the HTML element this message applies to.</param>
|
/// <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="viewPath">The path to the view.</param>
|
||||||
/// <param name="partialMatches">The set of modes with partial required attributes.</param>
|
/// <param name="partialMatches">The set of modes with partial required attributes.</param>
|
||||||
public PartialModeMatchLoggerStructure(
|
public PartialModeMatchLogValues(
|
||||||
string uniqueId,
|
string uniqueId,
|
||||||
string viewPath,
|
string viewPath,
|
||||||
[NotNull] IEnumerable<ModeMatchAttributes<TMode>> partialMatches)
|
[NotNull] IEnumerable<ModeMatchAttributes<TMode>> partialMatches)
|
||||||
: base(values: new Dictionary<string, object>
|
|
||||||
{
|
|
||||||
["UniqueId"] = uniqueId,
|
|
||||||
["ViewPath"] = viewPath,
|
|
||||||
["PartialMatches"] = partialMatches
|
|
||||||
})
|
|
||||||
{
|
{
|
||||||
_uniqueId = uniqueId;
|
_uniqueId = uniqueId;
|
||||||
_viewPath = viewPath;
|
_viewPath = viewPath;
|
||||||
_partialMatches = partialMatches;
|
_partialMatches = partialMatches;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public override string ToString()
|
||||||
/// Generates a human readable string for this structured log message.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The message.</returns>
|
|
||||||
public override string Format()
|
|
||||||
{
|
{
|
||||||
var newLine = Environment.NewLine;
|
var newLine = Environment.NewLine;
|
||||||
return string.Format(
|
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.Join($"{newLine}\t", _partialMatches.Select(partial =>
|
||||||
string.Format($"Mode '{partial.Mode}' missing attributes:{newLine}\t\t{{0}} ",
|
string.Format($"Mode '{partial.Mode}' missing attributes:{newLine}\t\t{{0}} ",
|
||||||
string.Join($"{newLine}\t\t", partial.MissingAttributes)))));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -182,11 +182,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
||||||
|
|
||||||
[Activate]
|
[Activate]
|
||||||
[HtmlAttributeNotBound]
|
[HtmlAttributeNotBound]
|
||||||
public ILoggerFactory LoggerFactory { get; set; }
|
protected internal ILogger<LinkTagHelper> Logger { get; set; }
|
||||||
|
|
||||||
// TODO: will remove LoggerFactory and activate logger once DI/hosting bug is fixed
|
|
||||||
[HtmlAttributeNotBound]
|
|
||||||
public ILogger<LinkTagHelper> Logger { get; set; }
|
|
||||||
|
|
||||||
[Activate]
|
[Activate]
|
||||||
[HtmlAttributeNotBound]
|
[HtmlAttributeNotBound]
|
||||||
|
|
@ -222,9 +218,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
||||||
|
|
||||||
var modeResult = AttributeMatcher.DetermineMode(context, ModeDetails);
|
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())
|
if (!modeResult.FullMatches.Any())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -151,11 +151,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
||||||
|
|
||||||
[Activate]
|
[Activate]
|
||||||
[HtmlAttributeNotBound]
|
[HtmlAttributeNotBound]
|
||||||
public ILoggerFactory LoggerFactory { get; set; }
|
protected internal ILogger<ScriptTagHelper> Logger { get; set; }
|
||||||
|
|
||||||
// TODO: will remove LoggerFactory and activate logger once DI/hosting bug is fixed
|
|
||||||
[HtmlAttributeNotBound]
|
|
||||||
public ILogger<ScriptTagHelper> Logger { get; set; }
|
|
||||||
|
|
||||||
[Activate]
|
[Activate]
|
||||||
[HtmlAttributeNotBound]
|
[HtmlAttributeNotBound]
|
||||||
|
|
@ -191,9 +187,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
||||||
|
|
||||||
var modeResult = AttributeMatcher.DetermineMode(context, ModeDetails);
|
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())
|
if (!modeResult.FullMatches.Any())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -411,13 +411,14 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
||||||
Assert.Equal(2, logger.Logged.Count);
|
Assert.Equal(2, logger.Logged.Count);
|
||||||
|
|
||||||
Assert.Equal(LogLevel.Warning, logger.Logged[0].LogLevel);
|
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.Equal(LogLevel.Verbose, logger.Logged[1].LogLevel);
|
||||||
Assert.IsAssignableFrom<ILogValues>(logger.Logged[1].State);
|
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());
|
((ILogValues)logger.Logged[1].State).ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -472,7 +473,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
|
||||||
|
|
||||||
Assert.Equal(LogLevel.Verbose, logger.Logged[0].LogLevel);
|
Assert.Equal(LogLevel.Verbose, logger.Logged[0].LogLevel);
|
||||||
Assert.IsAssignableFrom<ILogValues>(logger.Logged[0].State);
|
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());
|
((ILogValues)logger.Logged[0].State).ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue