-
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
Fix the boxing when using the LoggerMessage.Define is used and values are converted ToString #50768
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @maryamariyan Issue DetailsDescriptionToday LoggerMessage.Define is a high performance logging API that aims to reduce the overhead of logging by pre-parsing format strings and parameters holes to log. Today the values are stored in a LogValues<T0, TN> struct to avoid boxing. We do all of this work only to eventually box these arguments and allocate an array to turn this into a string. We should avoid the Regression?No Datausing System;
using Microsoft.Extensions.Logging;
namespace ConsoleApp105
{
class Program
{
static void Main(string[] args)
{
var callback = LoggerMessage.Define<int, int>(LogLevel.Information, new(0, "Event"), "This is a {Value} and a {Score}");
var logger = new Logger();
Console.WriteLine("Waiting");
Console.ReadLine();
for (int i = 0; i < 10000; i++)
{
callback(logger, 1, 0, null);
}
Console.WriteLine("Done");
Console.ReadLine();
}
public class Logger : ILogger
{
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var value = formatter(state, exception);
LogToSomeplace(value);
}
private void LogToSomeplace(string value)
{
}
}
}
}
When I add a 3rd parameter:
AnalysisWe need to implement non-allocating formatting for these calls runtime/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogValuesFormatter.cs Lines 158 to 189 in 89498d8
|
As these methods go down to
As immediate solution one could special case the most common value-types (int, long, DateTime{Offset}, Guid) and have special code for formatting to string / Span and use the ValueStringBuilder. But this is quite a bit of effort, though. |
@gfoidl See #50330 (comment). We might be able to use this implementation inside of logging. cc @geeknoid |
Did some profiling to see if we fixed this because of the new usage of CompositeFormat in the logging codebase. It turns out that we're still calling TryFormatArgumentIfNullOrEnumerable Which boxes the incoming arguments. Seems like a trivial fix and then we can close this! |
Description
Today LoggerMessage.Define is a high performance logging API that aims to reduce the overhead of logging by pre-parsing format strings and parameters holes to log. Today the values are stored in a LogValues<T0,...,TN> struct to avoid boxing. We do all of this work only to eventually box these arguments and allocate an array to turn this into a string. We should avoid the
object[]
and the boxing here.Regression?
No
Data
When I add a 4 int parameters:
Analysis
We need to implement non-allocating formatting for these calls
runtime/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogValuesFormatter.cs
Lines 158 to 189 in 89498d8
The text was updated successfully, but these errors were encountered: