Repository navigation
Closed
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-Loggingfeature-request
Milestone
Description
Problem Description
In a large codebase, there are cases people want to log with existing types defined for business logic (and these types might come from a 3rd party library which is not controlled by the dev), rather than having to invent another type (and do the mapping) or write lots of boilerplate code.
public struct Food
{
public string Name { get; set; }
public double Price { get; set; }
}
logger.Eat(new Food { Name = "artichoke", Price = 3.99 });In order for the above code to work, one has to write the boilerplate code as follows, and will start to hit performance issue if there are more than 6 fields (based on the current implementation).
using System;
using Microsoft.Extensions.Logging;
public static class LoggerExtensions
{
private static readonly Action<ILogger, Food, Exception> _eat = LoggerMessage.Define<Food>(
LogLevel.Information,
new EventId(1, nameof(Eat)),
"Eat (Food = {food})."); // message template, not interpolated string
public static void Eat(this ILogger logger, Food food)
{
_eat(logger, food, null);
}
}The Ask
Provide a vendor agnostic way for the user to log strong typed objects without having to write boilerplate code, yet with good/reasonable performance.
public struct Food
{
public string Name { get; set; }
public double Price { get; set; }
}
public class Dog
{
public string Name { get; set; }
public int Age { get; set; }
}
logger.Eat(new Food { Name = "Artichoke", Price = 3.99 });
logger.Log(new Dog { Name = "Labrador Retriever", Age = 2 });Additional Information
- It is not clear if this should part of .NET runtime, or it should be provided by 3rd party (e.g. OpenTelemetry). I guess one goal could be - any existing provider (e.g.
ConsoleLoggerProvider) should just work. - This potentially could leverage compile-time logging source generation.
- Additional scenario 1
- Additional scenario 2
Reacted by Cijo Thomas, aetos, Tom Tan, Soheil Alizadeh, Hannah Chan, Kai, Mohamed-Effat and Alex Bondarenko
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-Loggingfeature-request
Activity
yecril71pl commentedon Dec 29, 2021
The introduction says that named parameters can be used to attach properties to log entries.
I would like the opposite syntax to work in the following scenarios:
All these calls should translate to
Implementing this using reflexion bumps into the following problem: support for enumerating parameter islands in a message format string, which was never officially supported, was removed from the public API in ASP.NET 3, so any independent solution would have to rely on undocumented features or contain a copy or a replica of internal code.
maryamariyan commentedon Feb 15, 2022
To add strong type support, consider the
Foodstruct in the issue description. If developer added an annotation likeStructuredLogMessageshown below to a partialFoostruct, the logging source generator could potentially code generate the remaining code portion.@davidfowl @reyang @noahfalk do you think if we added this to the logging source generator it would be something that could be adopted for open telemetry as-is?
14 remaining items
KalleOlaviNiemitalo commentedon Feb 22, 2022
Would LogMessage<> be an interface type there, or are you proposing a C# change as well?
noahfalk commentedon Feb 22, 2022
No intent to propose a C# change I think (I was part of the offline discussion that came from), it was just handwavy brainstorming. I'm hoping that portion of the problem is sufficiently solved by the pre-existing source generator work that we can do nothing.
Different designs might do portions of the serialization work at different stages, for example between the source generator and ILogger, between the ILogger implementation and ILoggerProvider, or in ILoggerProvider. I am hoping we don't need to consider implementation concerns quite yet though. My suggestion is to figure out if users could wave a magic wand at their logging code to serialize data however they wished, what would that serialized output look like?
yecril71pl commentedon Feb 23, 2022
The text must not be too long (unless a dedicated log book is used). It should contain key information that is specific, variable and hard to deduce from the context. If there is too much key information, there is a chance that the object itself is too rich and should be split.
tbolon commentedon Apr 19, 2022
I am not sure if it is really related to your use case, but Serilog (which is one of the defacto logging library for .net) supports '@' prefix in parameter names in logs, and "destructure" the object into the output message:
renders:
Tools such as seq can process such objects and make them useable when searching logs.
Obviously, it heavily makes use of reflection.
I suppose your goal here is to make more explicit choices about what to log, and get rid of any reflection cost. So my comment will only be there to be sure that you are aware of the ecosystem about object logging :)
p10tyr commentedon May 12, 2022
This is not strictly about Strong type support but a request to allow us to add our own Attributes to the LogRecord class
I understand the States get enumerated and pushed onto repeated opentelemetry.proto.common.v1.KeyValue attributes with the exporter.
But we have absolutely no way to add our own attributes here when ParseStateValues is false or true.
The use case in my situation is that I always want to add a tenant attribute to everything Log and Trace for grouping in the APM. I can create my own BaseProcessor<LogRecord and add it as a Processor to LogBuilder.. But honestly.. its quite pointless because I cant add attributes .. easily.
Even if I were to hand roll out or customise OpentTelemetryLogProvider the OTLPLoggerClass does not really accept any kind of extra attributes.
It just assumes you are going to use Scopes (which is put on the Scopes Proto Attribute any way) or Provide States which is impractical to do and police on every single line that tries to log.
If LogRecord has something like .AddAtrribute(KeyValue) and OpenTelemetryLogger passed it along it would be easy to add what is needed with a processor. (Even if Scopes and States are turned off)
--Edit
I was helped out with an example on how to leverage Scopes to achieve what I needed. I was just stuck in my own tunnel vision.
Thank you for the great work on OTLP (and sorry for hijacking this thread a bit)
I am definitely going to follow this as it does look like a really awesome feature to have!
obondarenko1 commentedon Jan 4, 2024
Subscribing to this issue, as not being able to have a structured object written out as a JSON using high-performance logging making subsequent log analysis a very unpleasant chore.
geeknoid commentedon Jan 4, 2024
Note that .NET 8 introduced https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute which knows how to log complex objects.
reyang commentedon Jan 11, 2024
Closing this issue since it was resolved by https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/README.md as mentioned here. Related to #81730.
Here is a simple example open-telemetry/opentelemetry-dotnet#5214.