-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Improved The Performance Of Logger #66895
Conversation
Updated LoggerExtensions.cs To Only Even Try To Log If The Log Level If Enable To Improve Performance Of Logger By Saving Memory Allocation
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsUpdated
|
Added A Space After The `if` Keyword On-Line `28` Inside `LoggerExtensions.cs`
@@ -25,7 +25,10 @@ public static class LoggerExtensions | |||
/// <example>logger.LogDebug(0, exception, "Error while processing request from {Address}", address)</example> | |||
public static void LogDebug(this ILogger logger, EventId eventId, Exception? exception, string? message, params object?[] args) | |||
{ | |||
logger.Log(LogLevel.Debug, eventId, exception, message, args); | |||
if (logger.IsEnabled(LogLevel.Debug)) |
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.
Which allocations should be saved by this pattern?
The logger.IsEnabled
-check should be done before the call to LogDebug
in user code.
Thus the allocation of args
(the array) and potentially the creation of message
can be saved. These objects are already existent when reaching the check you introduced here.
Or even better use LoggerMessage.Define where that behavior can be set / use the (new) logging source generator.
@ProgrammingFire please provide the benchmark numbers for the allocation and the code you are using to measure it. |
Updated `LoggerExtensions.cs` To Only Even Try To Log If The Log Level Is Enabled And Make Sure The First Three Arguments For The Formatted Message Is Passed As Generics To Make Sure They Are Not Boxed And Unboxed To Improve Performance Of Logger By Saving Memory Allocation
Check this new commit which actually improves the memory allocation by not storing the first three arguments into objects so that they are not boxed and unboxed that actually saves memory allocation: cc87f19 |
I think the improvements in the logger depends on the user that's why I am closing this pull request. |
Updated
LoggerExtensions.cs
To Only Even Try To Log If The Log Level Is Enable To Improve Performance Of Logger By Saving Memory Allocation