Skip to content

Avoid some Logger instantiations per request#50184

Merged
adityamandaleeka merged 1 commit intodotnet:mainfrom
stephentoub:loggeralloc
Aug 21, 2023
Merged

Avoid some Logger instantiations per request#50184
adityamandaleeka merged 1 commit intodotnet:mainfrom
stephentoub:loggeralloc

Conversation

@stephentoub
Copy link
Member

CreateLogger<T> returns an ILogger<T> and always allocates a new Logger<T> instance. CreateLogger(typeof(T)) returns a non-generic ILogger and delegates to the factory's CreateLogger, thus typically not allocating a new one.

`CreateLogger<T>` always creates a new `Logger<T>` instance. `CreateLogger(typeof(T))` delegates to the factory, and thus typically doesn't.
@stephentoub stephentoub requested review from a team, BrennanConroy and halter73 as code owners August 18, 2023 13:54
@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Aug 18, 2023
@stephentoub
Copy link
Member Author

stephentoub commented Aug 18, 2023

Isn't it possible to modify the generic overload implementation to have the same behavior as the non-generic one?

The generic one is statically typed to return ILogger<T>:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loggerfactoryextensions.createlogger?view=dotnet-plat-ext-7.0#microsoft-extensions-logging-loggerfactoryextensions-createlogger-1(microsoft-extensions-logging-iloggerfactory)

LoggerFactory.CreateLogger returns ILogger, and the instances it creates almost never implement ILogger<T>.

It seems very simple to modify the implementation of the generic version to delegate the creation of the logger to the factory as well.

Please share.

@stephentoub
Copy link
Member Author

Sorry, I didn't see the return type is not the same. Can we not modify the implementation of the generic version to cache the logger (like the non-generic version)?

Technically, sure, it could have a thread-safe cache per T, indexed by ILogger and storing an ILogger<T> that wraps that ILogger, but it would also need to be throttled in some way, as if the underlying CreateLogger always produced a new instance, the cache could grow unbounded. That's not trivial to get right, in particular around cache eviction policies. The 95% use case for this method, as well, in situations where the extra allocation doesn't matter: you'll note that this PR didn't change the vast majority of uses of CreateLogger<T>, only ones where it's used in a manner where the caller itself isn't caching... most of the time, a caller stores the resulting instance into a field of something longer-lived.

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

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants