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

Log diagnostic info about search for localization resources #331

Merged
merged 1 commit into from
Feb 24, 2017

Conversation

ryanbrandenburg
Copy link
Contributor

Fixes #258.

@@ -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; }
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryanbrandenburg - SearchedLocation?

Copy link
Contributor

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; }
Copy link
Contributor

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; }
Copy link
Contributor

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;
Copy link
Contributor

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-*" />
Copy link
Contributor

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;
Copy link
Contributor

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();
Copy link
Contributor

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;
Copy link
Contributor

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
Copy link
Contributor

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()
Copy link
Contributor

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.
Copy link
Contributor

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.

@ryanbrandenburg
Copy link
Contributor Author

🆙📅

Copy link
Contributor

@pranavkm pranavkm left a 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-*" />
Copy link
Contributor

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();
Copy link
Contributor

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?

Copy link
Contributor Author

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}}'.");
Copy link
Contributor

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());
Copy link
Contributor

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>
Copy link
Contributor

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.
Copy link
Contributor

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;
Copy link
Contributor

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;

@ryanbrandenburg ryanbrandenburg merged commit 39aa943 into dev Feb 24, 2017
@JunTaoLuo JunTaoLuo deleted the rybrande/DiagnosticInfo branch May 23, 2017 19:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants