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

Commit 0a1918a

Browse files
committed
Cleanup logging in tag helpers
1 parent f2fed5e commit 0a1918a

File tree

6 files changed

+32
-88
lines changed

6 files changed

+32
-88
lines changed

src/Microsoft.AspNet.Mvc.TagHelpers/Internal/ModeMatchResult.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ public void LogDetails<TTagHelper>(
5555
attribute => PartiallyMatchedAttributes.Contains(
5656
attribute, StringComparer.OrdinalIgnoreCase)));
5757

58-
logger.LogWarning(new PartialModeMatchLoggerStructure<TMode>(uniqueId, viewPath, partialOnlyMatches));
58+
logger.LogWarning(new PartialModeMatchLogValues<TMode>(uniqueId, viewPath, partialOnlyMatches));
5959
}
6060

6161
if (logger.IsEnabled(LogLevel.Verbose) && !FullMatches.Any())
6262
{
63-
logger.LogVerbose("Skipping processing for {0} {1}",
64-
tagHelper.GetType().GetTypeInfo().FullName, uniqueId);
63+
logger.LogVerbose(
64+
"Skipping processing for tag helper '{TagHelper}' with id '{TagHelperId}'.",
65+
tagHelper.GetType().GetTypeInfo().FullName,
66+
uniqueId);
6567
}
6668
}
6769
}

src/Microsoft.AspNet.Mvc.TagHelpers/Internal/PartialAttributeLoggerStructureOfT.cs renamed to src/Microsoft.AspNet.Mvc.TagHelpers/Internal/PartialModeMatchLogValuesOfT.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,50 @@
1111
namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
1212
{
1313
/// <summary>
14-
/// An <see cref="ILogValues"/> for log messages regarding <see cref="ITagHelper"/> instances that opt out of
14+
/// Log values for <see cref="ITagHelper"/> instances that opt out of
1515
/// processing due to missing attributes for one of several possible modes.
1616
/// </summary>
17-
public class PartialModeMatchLoggerStructure<TMode> : PartialModeMatchLoggerStructure
17+
public class PartialModeMatchLogValues<TMode> : ILogValues
1818
{
1919
private readonly string _uniqueId;
2020
private readonly string _viewPath;
2121
private readonly IEnumerable<ModeMatchAttributes<TMode>> _partialMatches;
2222

2323
/// <summary>
24-
/// Creates a new <see cref="PartialModeMatchLoggerStructure{TMode}"/>.
24+
/// Creates a new <see cref="PartialModeMatchLogValues{TMode}"/>.
2525
/// </summary>
2626
/// <param name="uniqueId">The unique ID of the HTML element this message applies to.</param>
2727
/// <param name="viewPath">The path to the view.</param>
2828
/// <param name="partialMatches">The set of modes with partial required attributes.</param>
29-
public PartialModeMatchLoggerStructure(
29+
public PartialModeMatchLogValues(
3030
string uniqueId,
3131
string viewPath,
3232
[NotNull] IEnumerable<ModeMatchAttributes<TMode>> partialMatches)
33-
: base(values: new Dictionary<string, object>
34-
{
35-
["UniqueId"] = uniqueId,
36-
["ViewPath"] = viewPath,
37-
["PartialMatches"] = partialMatches
38-
})
3933
{
4034
_uniqueId = uniqueId;
4135
_viewPath = viewPath;
4236
_partialMatches = partialMatches;
4337
}
44-
45-
/// <summary>
46-
/// Generates a human readable string for this structured log message.
47-
/// </summary>
48-
/// <returns>The message.</returns>
49-
public override string Format()
38+
39+
public override string ToString()
5040
{
5141
var newLine = Environment.NewLine;
5242
return string.Format(
53-
$"Tag Helper with ID {_uniqueId} in view '{_viewPath}' had partial matches while determining mode:{newLine}\t{{0}}",
43+
$"Tag Helper with ID '{_uniqueId}' in view '{_viewPath}' had partial matches " +
44+
$"while determining mode:{newLine}\t{{0}}",
5445
string.Join($"{newLine}\t", _partialMatches.Select(partial =>
5546
string.Format($"Mode '{partial.Mode}' missing attributes:{newLine}\t\t{{0}} ",
5647
string.Join($"{newLine}\t\t", partial.MissingAttributes)))));
5748
}
49+
50+
public IEnumerable<KeyValuePair<string, object>> GetValues()
51+
{
52+
yield return new KeyValuePair<string, object>(
53+
"Message",
54+
"Tag helper had partial matches while determining mode.");
55+
yield return new KeyValuePair<string, object>("UniqueId", _uniqueId);
56+
yield return new KeyValuePair<string, object>("ViewPath", _viewPath);
57+
yield return new KeyValuePair<string, object>("PartialMatches", _partialMatches);
58+
}
5859
}
5960
}

src/Microsoft.AspNet.Mvc.TagHelpers/Internal/PartialModeMatchLoggerStructure.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,7 @@ private enum Mode
182182

183183
[Activate]
184184
[HtmlAttributeNotBound]
185-
public ILoggerFactory LoggerFactory { get; set; }
186-
187-
// TODO: will remove LoggerFactory and activate logger once DI/hosting bug is fixed
188-
[HtmlAttributeNotBound]
189-
public ILogger<LinkTagHelper> Logger { get; set; }
185+
protected internal ILogger<LinkTagHelper> Logger { get; set; }
190186

191187
[Activate]
192188
[HtmlAttributeNotBound]
@@ -222,9 +218,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
222218

223219
var modeResult = AttributeMatcher.DetermineMode(context, ModeDetails);
224220

225-
var logger = Logger ?? LoggerFactory.CreateLogger<LinkTagHelper>();
226-
227-
modeResult.LogDetails(logger, this, context.UniqueId, ViewContext.View.Path);
221+
modeResult.LogDetails(Logger, this, context.UniqueId, ViewContext.View.Path);
228222

229223
if (!modeResult.FullMatches.Any())
230224
{

src/Microsoft.AspNet.Mvc.TagHelpers/ScriptTagHelper.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,7 @@ private enum Mode
151151

152152
[Activate]
153153
[HtmlAttributeNotBound]
154-
public ILoggerFactory LoggerFactory { get; set; }
155-
156-
// TODO: will remove LoggerFactory and activate logger once DI/hosting bug is fixed
157-
[HtmlAttributeNotBound]
158-
public ILogger<ScriptTagHelper> Logger { get; set; }
154+
protected internal ILogger<ScriptTagHelper> Logger { get; set; }
159155

160156
[Activate]
161157
[HtmlAttributeNotBound]
@@ -191,9 +187,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
191187

192188
var modeResult = AttributeMatcher.DetermineMode(context, ModeDetails);
193189

194-
var logger = Logger ?? LoggerFactory.CreateLogger<ScriptTagHelper>();
195-
196-
modeResult.LogDetails(logger, this, context.UniqueId, ViewContext.View.Path);
190+
modeResult.LogDetails(Logger, this, context.UniqueId, ViewContext.View.Path);
197191

198192
if (!modeResult.FullMatches.Any())
199193
{

test/Microsoft.AspNet.Mvc.TagHelpers.Test/ScriptTagHelperTest.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,14 @@ public async Task LogsWhenARequiredAttributeIsMissing(
411411
Assert.Equal(2, logger.Logged.Count);
412412

413413
Assert.Equal(LogLevel.Warning, logger.Logged[0].LogLevel);
414-
Assert.IsAssignableFrom<PartialModeMatchLoggerStructure>(logger.Logged[0].State);
414+
Assert.IsAssignableFrom<ILogValues>(logger.Logged[0].State);
415415

416-
var loggerData0 = (PartialModeMatchLoggerStructure)logger.Logged[0].State;
416+
var loggerData0 = (ILogValues)logger.Logged[0].State;
417417

418418
Assert.Equal(LogLevel.Verbose, logger.Logged[1].LogLevel);
419419
Assert.IsAssignableFrom<ILogValues>(logger.Logged[1].State);
420-
Assert.StartsWith("Skipping processing for Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper",
420+
Assert.StartsWith("Skipping processing for tag helper 'Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper'" +
421+
" with id",
421422
((ILogValues)logger.Logged[1].State).ToString());
422423
}
423424

@@ -472,7 +473,7 @@ public async Task LogsWhenAllRequiredAttributesAreMissing()
472473

473474
Assert.Equal(LogLevel.Verbose, logger.Logged[0].LogLevel);
474475
Assert.IsAssignableFrom<ILogValues>(logger.Logged[0].State);
475-
Assert.StartsWith("Skipping processing for Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper",
476+
Assert.StartsWith("Skipping processing for tag helper 'Microsoft.AspNet.Mvc.TagHelpers.ScriptTagHelper'",
476477
((ILogValues)logger.Logged[0].State).ToString());
477478
}
478479

0 commit comments

Comments
 (0)