-
Notifications
You must be signed in to change notification settings - Fork 65
Log diagnostic info about search for localization resources #331
Conversation
@@ -64,6 +77,11 @@ public LocalizedString(string name, string value, bool resourceNotFound) | |||
public bool ResourceNotFound { get; } | |||
|
|||
/// <summary> | |||
/// The location which was searched for a locatization value. | |||
/// </summary> | |||
public string LocationSearched { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still wonder why we have this per LocalizedString
while the searched location is common for all of them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hishamco - isn't that the behavior of the ResourceManagerStringLocalizer
and not necessarily a trait of this type? Plus the intent of making this change was to help diagnosing given that you have a localized string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryanbrandenburg - SearchedLocation
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't said its the behavior of ResourceManagerStringLocalizer
😃 , even the intent of making this change was to help diagnosing given that you have a localized string. I'm still disagree to add the LocationSearched
or SearchedLocation
to the LocalizedString
itself, if we think deeply for a minute the ResourceNotFound
is suitable here, because we may find the resource or might not, but the SearchedLocation
is something generic for all the resources
I will try to create a branch in my localization repo to prove the concept
@@ -64,6 +77,11 @@ public LocalizedString(string name, string value, bool resourceNotFound) | |||
public bool ResourceNotFound { get; } | |||
|
|||
/// <summary> | |||
/// The location which was searched for a locatization value. | |||
/// </summary> | |||
public string LocationSearched { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hishamco - isn't that the behavior of the ResourceManagerStringLocalizer
and not necessarily a trait of this type? Plus the intent of making this change was to help diagnosing given that you have a localized string.
@@ -64,6 +77,11 @@ public LocalizedString(string name, string value, bool resourceNotFound) | |||
public bool ResourceNotFound { get; } | |||
|
|||
/// <summary> | |||
/// The location which was searched for a locatization value. | |||
/// </summary> | |||
public string LocationSearched { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryanbrandenburg - SearchedLocation
?
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using System; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sort
@@ -15,6 +15,7 @@ | |||
<ProjectReference Include="..\Microsoft.Extensions.Localization.Abstractions\Microsoft.Extensions.Localization.Abstractions.csproj" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.2.0-*" /> | |||
<PackageReference Include="Microsoft.Extensions.Options" Version="1.2.0-*" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.2.0-*" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging.Abstractions
_resourceStringProvider = resourceStringProvider; | ||
_resourceManager = resourceManager; | ||
_resourceBaseName = baseName; | ||
_resourceNamesCache = resourceNamesCache; | ||
_loggerFactory = loggerFactory; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need this field.
@@ -75,7 +80,8 @@ public void Create_FromType_ReturnsCachedResultForSameType() | |||
var locOptions = new LocalizationOptions(); | |||
var options = new Mock<IOptions<LocalizationOptions>>(); | |||
options.Setup(o => o.Value).Returns(locOptions); | |||
var factory = new ResourceManagerStringLocalizerFactory(localizationOptions: options.Object); | |||
var loggerFactory = GetLoggerFactory(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use NullLoggerFactory.Instance
@@ -1,13 +1,14 @@ | |||
// 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 Microsoft.Extensions.Localization.Internal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sort. System namespaces first.
} | ||
} | ||
|
||
public class TestLoggerFactory : ILoggerFactory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Microsoft.Extensions.Logging.Testing.TestLoggerFactory
.
@@ -77,6 +84,56 @@ public void EnumeratorCacheIsScopedByAssembly() | |||
Assert.Equal(expectedCallCount, resourceAssembly2.GetManifestResourceStreamCallCount); | |||
} | |||
|
|||
[Fact] | |||
public void ResourceManagerStringLocalizer_GetString_LocationSearchedPopulates() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetString_PopulatesSearchedLocationOnLocalizedString
loggerFactory); | ||
|
||
// Act | ||
// We have to access the result so it evaluates. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment doesn't really make sense.
4fe6a2a
to
c6e4a69
Compare
🆙📅 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sure you remove the .Testing reference from the src project. We definitely don't want that.
@@ -15,6 +15,8 @@ | |||
<ProjectReference Include="..\Microsoft.Extensions.Localization.Abstractions\Microsoft.Extensions.Localization.Abstractions.csproj" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.2.0-*" /> | |||
<PackageReference Include="Microsoft.Extensions.Options" Version="1.2.0-*" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.2.0-*" /> | |||
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="1.2.0-*" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong project?
@@ -162,6 +226,17 @@ private static int GetCultureInfoDepth(CultureInfo culture) | |||
return result; | |||
} | |||
|
|||
|
|||
private TestSink _sink = new TestSink(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need to go at the top of the class. Also
private readonly TestSink _sink = new TestSink();
private readonly ILogger Logger { get; } = ew TestLoggerFactory(_sink, true).CreateLogger<ResourceManagerStringLocalizer>();
^ does the second line work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunatly no, you get "A field initializer cannot reference the non-static field, method or property '_sink'".
_searchedLocation = LoggerMessage.Define<string, string, CultureInfo>( | ||
LogLevel.Debug, | ||
1, | ||
$"{nameof(ResourceManagerStringLocalizer)} searched for '{{Key}}' in '{{LocationSearched}}' with '{{Culture}}'."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with culture `{{Culture}}'
|
||
// Assert | ||
Assert.Equal(1, Sink.Writes.Count); | ||
Assert.Equal("ResourceManagerStringLocalizer searched for 'a key!' in 'Resources.TestResource' with 'en-US'.", Sink.Writes.First().State.ToString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with culture 'en-US'.
/// <param name="name">The name of the string in the resource it was loaded from.</param> | ||
/// <param name="value">The actual string.</param> | ||
/// <param name="resourceNotFound">Whether the string was not found in a resource. Set this to <c>true</c> to indicate an alternate string value was used.</param> | ||
/// <param name="locationSearched">The location which was searched for a locatization value.</param> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo in locatization
@@ -64,6 +77,11 @@ public LocalizedString(string name, string value, bool resourceNotFound) | |||
public bool ResourceNotFound { get; } | |||
|
|||
/// <summary> | |||
/// The location which was searched for a locatization value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo is hard during copy & past 😄
@@ -7,6 +7,7 @@ | |||
using System.Reflection; | |||
using System.Resources; | |||
using Microsoft.Extensions.Options; | |||
using Microsoft.Extensions.Logging; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should go before
using Microsoft.Extensions.Options;
a256177
to
7a8d74f
Compare
Fixes #258.