Skip to content

Log whether the localization resource is found or not - #55897

Merged
halter73 merged 9 commits into
dotnet:mainfrom
hishamco:localization-resource-existence
Aug 7, 2026
Merged

halter73 merged 9 commits into
dotnet:mainfrom
hishamco:localization-resource-existence

Conversation

@hishamco

Copy link
Copy Markdown
Member

Fixes #43109

@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label May 27, 2024
@hishamco
hishamco requested a review from halter73 May 27, 2024 03:05
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 27, 2024
@mkArtakMSFT mkArtakMSFT added area-ui-rendering Includes: MVC Views, Razor Pages and removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 28, 2024
@mkArtakMSFT

Copy link
Copy Markdown
Contributor

@halter73 can you please review this? Thanks!

{
_missingManifestCache.TryAdd(cacheKey, null);

Log.ResourceNotFound(_logger, name);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we provide a different log for a missing manifest vs a non-localized resource where the name == value?

@DamianEdwards do you have an opinion on this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No real opinion, I'd defer to folks who've been using it for real to inform us to how useful that would be.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

IMHO we should log for both

@halter73 halter73 May 31, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree we should log in both cases. I was asking if there should be a different log message for the two places this PR currently calls Log.ResourceNotFound since the resource was not found for slightly different reasons, but I'm fine with using the same message whenever a resource is not found for any reason like the PR does currently.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Or we could change the log level for missing manifest to trace so in debug the missing resources will be logged which are useful in almost cases

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd just leave them exactly the same for now. If we were going to change the log level, we'd want to give it a unique event id and log message. Changing the level of a log but nothing else would be confusing.

@halter73 halter73 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given that the SearchedLocation log exists, I wonder if we need a ResourceFound log. A lack of a ResourceNotFound log should imply that it was found. This would also mean less debug log noise for a fully localized application which seems like a good thing.

[LoggerMessage(2, LogLevel.Debug, $"The resource with key '{{Key}}' is not found.", EventName = "ResourceNotFound")]
public static partial void ResourceNotFound(ILogger logger, string key);

[LoggerMessage(3, LogLevel.Debug, $"The resource with key '{{Key}}' is found.", EventName = "ResourceFound")]

@halter73 halter73 May 30, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
[LoggerMessage(3, LogLevel.Debug, $"The resource with key '{{Key}}' is found.", EventName = "ResourceFound")]

Edit: I'd rather we remove this log altogether.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So, you will the non found resources?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure I understand your question, but I'm asking that we only add the "ResourceNotFound" log not the "ResourceFound" log.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Forgot my above comment seems I miss some words during the writing :)

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jun 8, 2024
@hishamco

hishamco commented Jun 8, 2024

Copy link
Copy Markdown
Member Author

@halter73 what's the status PR? is there anything to add to merge it ASAP

@halter73

Copy link
Copy Markdown
Member

Yes. I would like to remove the "ResourceFound" logs and pass in the "Culture" parameter to Log.ResourceNotFound(_logger, name, keyCulture);. Not doing so is currently causing build errors now that the log message includes {{Culture}} in it.

@halter73

halter73 commented Jul 8, 2024

Copy link
Copy Markdown
Member

@hishamco Do you think you can fix the build and test failures? We should also add a test case that checks the logs for $"A resource for '{{Key}}' with culture '{{Culture}}' was not found.".

@danmoseley

Copy link
Copy Markdown
Contributor

@hishamco I'm going to close this PR due to lack of activity -- you're welcome to reopen if and when you're ready to finish it. Thanks!

@hishamco
hishamco force-pushed the localization-resource-existence branch from c251d3a to f3aecda Compare July 29, 2026 06:34
Copilot AI review requested due to automatic review settings July 29, 2026 06:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a dedicated debug log event when ResourceManagerStringLocalizer cannot resolve a string resource, helping developers identify missing localizations (Fixes #43109).

Changes:

  • Log a new ResourceNotFound debug message when ResourceManager.GetString returns null, when a manifest is missing, and when a missing-manifest cache entry is hit.
  • Add/extend unit tests to validate ResourceNotFound logging behavior and culture handling.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Localization/Localization/src/ResourceManagerStringLocalizer.cs Adds a new ResourceNotFound LoggerMessage and emits it on missing resources / missing manifest scenarios.
src/Localization/Localization/test/Microsoft.Extensions.Localization.Tests/ResourceManagerStringLocalizerTest.cs Adds tests for the new log event and updates existing logging assertions.
Comments suppressed due to low confidence (2)

src/Localization/Localization/test/Microsoft.Extensions.Localization.Tests/ResourceManagerStringLocalizerTest.cs:304

  • Test name says "Once" but the test performs two lookups and asserts two ResourceNotFound log entries. Rename the test to avoid the misleading expectation.
    public void GetString_LogsResourceNotFoundOnce_WhenManifestIsMissing()

src/Localization/Localization/test/Microsoft.Extensions.Localization.Tests/ResourceManagerStringLocalizerTest.cs:145

  • This assertion block assumes a second log entry that doesn't exist in ResourceManagerStringLocalizer (there is no "resource found" log). To keep this test stable and focused, assert only on the SearchedLocation event/message.
        var write = Sink.Writes;
        Assert.Equal(2, write.Count);
        Assert.Equal("ResourceManagerStringLocalizer searched for 'a key!' in 'Resources.TestResource' with culture 'en-US'.", write.ElementAt(0).State.ToString());
        Assert.Equal("The resource with key 'a key!' is found.", write.ElementAt(1).State.ToString());
    }

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/Localization/Localization/test/Microsoft.Extensions.Localization.Tests/ResourceManagerStringLocalizerTest.cs:303

  • The test name says the ResourceNotFound message is logged "Once", but the test calls the localizer twice and asserts there are 2 ResourceNotFound writes (one per call). Rename the test to match the asserted behavior (or adjust the assertion if the intent was truly once per key/culture).
    public void GetString_LogsResourceNotFoundOnce_WhenManifestIsMissing()

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@halter73 halter73 modified the milestones: 10.0-preview1, 11.0-rc1 Aug 7, 2026
@halter73 halter73 removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Aug 7, 2026

@halter73 halter73 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for getting back to this after all this time!

@halter73
halter73 merged commit f0e6aa6 into dotnet:main Aug 7, 2026
24 checks passed
@hishamco

hishamco commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

@halter73 I forgot it, and Orchard Core keeps me busy :)

Thanks

@hishamco

hishamco commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

Will this be part of .NET 11 or 12?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ui-rendering Includes: MVC Views, Razor Pages community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log a message when ResourceManagerStringLocalizer cannot find a resource

6 participants