Skip to content

Use LoggerMessage in HttpAbstractions #33927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2021
Merged

Conversation

pranavkm
Copy link
Contributor

No description provided.

@ghost ghost added the area-runtime label Jun 29, 2021
@pranavkm pranavkm requested a review from JamesNK June 29, 2021 02:39
@pranavkm pranavkm force-pushed the prkrishn/use-logger-message branch from 631bf68 to 48d731b Compare July 5, 2021 18:47
Comment on lines +352 to +358
private static partial class Log
{
[LoggerMessage(1, LogLevel.Debug,
"Request successfully matched the route with name '{RouteName}' and template '{RouteTemplate}'",
EventName = "RequestMatchedRoute")]
public static partial void RequestMatchedRoute(ILogger logger, string? routeName, string? routeTemplate);
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should be duplicating private log types. If logging is shared by multiple types, then a non-nested internal static class is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realize I'm slightly contradicting what I said earlier here: https://github.com/dotnet/aspnetcore/pull/33927/files#r660733847, but if we were to write the code from scratch, it's very unlikely we'll decide to try and look for ways to commonize logging statements. I think it's much more intuitive to pick a pattern (use private log types) and use it consistently. We can definitely have outs for when the log has to perform a bunch of work, but for a simple one like this, it just seems like more work to try and de-dupe.

Copy link
Member

Choose a reason for hiding this comment

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

I disagree. Splitting log types up for a single ILogger isn't a good idea. It makes it easy to unknowingly introduce duplicate IDs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Splitting log types up for a single ILogger isn't a good idea

Ok, I'm confused. I thought these were two different types that happened to be using the same logger message because they both wanted to do the same thing i.e. TreeRouter and RouterBase both happened to want to log the same message and so used the same LoggerMessage.

If that is the case (which I think it is), consider writing a new FooRouter that needs to log. Do we trawl thru all of the existing logger extensions to see if there's an existing method that's a good fit? The source generator will complain if you re-use event ids. Also what happens when FooRouter wants to log something that doesn't already exist? Do we put it in the shared static class so that the analyzer can ensure event-ids don't get duplicated?

Copy link
Member

Choose a reason for hiding this comment

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

I took a look at what exactly is happening with these types and their loggers and saw they have distinct category names. In that case, I think splitting them up like this is fine. My concern was they shared a name but that isn't the case.

Here is my thinking on private Log types:

If the ILogger instance is private to a type (i.e. the type is passed an ILoggerFactory and creates its own logger using its type name) then it makes sense that its Log type is nested and private. If that isn't the case - the logger instance is shared by multiple classes - then a regular static class that can be shared by all the types that use the logger is better.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Yes I like this pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think?

Sure, that seems reasonable. Are there actual examples of this? I'm mostly familiar with MVC doing the former (re-using the same message across different logger categories), but not the latter.

@pranavkm pranavkm force-pushed the prkrishn/use-logger-message branch from 48d731b to 7cfff88 Compare July 7, 2021 17:47
@pranavkm pranavkm enabled auto-merge (squash) July 7, 2021 17:47
private readonly static Func<IFeatureCollection, IHttpResponseFeature?> _nullResponseFeature = f => null;
private readonly static Func<IFeatureCollection, IHttpResponseBodyFeature?> _nullResponseBodyFeature = f => null;
private readonly static Func<IFeatureCollection, IResponseCookiesFeature?> _newResponseCookiesFeature = f => new ResponseCookiesFeature(f);
private static readonly Func<IFeatureCollection, IHttpResponseFeature?> _nullResponseFeature = f => null;
Copy link
Member

Choose a reason for hiding this comment

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

That's pretty pedantic, I like it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pranavkm
Copy link
Contributor Author

pranavkm commented Jul 7, 2021

This is updated. Could I have someone take another look?

@pranavkm pranavkm merged commit 92975b3 into main Jul 8, 2021
@pranavkm pranavkm deleted the prkrishn/use-logger-message branch July 8, 2021 18:15
@ghost ghost added this to the 6.0-preview7 milestone Jul 8, 2021
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants