extensions: LoggerMessageAttribute doesn't support parameters with dots
Description
Hi,
first of all, I really like the LoggerMessageAttribute š, but I have a āsmallā problem why I canāt use it: In order to align with our custom tags from our OpenTelemetry traces we want to use parameters with dots in the name. Something like this āmessaging.idā or āmessaging.typeā.
Itās not possible to do that with the LoggerMessageAttribute as itās not possible to include a ādotā in the c# parameter or did I missed something?
My issue is similar to https://github.com/dotnet/runtime/issues/60968, but itās not the same as my example is about a dot and not a @.
Reproduction Steps
This doesnāt compile:
[LoggerMessage(Level = LogLevel.Information, EventName = "LogMessage",
Message = "The event {messaging.id} of type {messaging.type} did something")]
public static partial void LogMyMessage(this ILogger logger, string messaging.id, string messaging.type);
If I try to use something do it like this the code generated from the code generator is not compiling:
[LoggerMessage(Level = LogLevel.Information, EventName = "LogMessage",
Message = "The event {messaging.id} of type {messaging.type} did something")]
public static partial void LogMyMessage(this ILogger logger, string messaging_id, string messaging_type);
Expected behavior
Be able to use it with a parameter that has a ādotā š
Actual behavior
Code is not compiling.
Regression?
No response
Known Workarounds
No response
Configuration
.Net 6.0
Other information
No response
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 27 (13 by maintainers)
@JWilh Microsoft.Extensions.Telemetry defines
TagNameAttribute, though it is marked as experimental.This is used in
Microsoft.Extensions.Http.Logging, to create OTEL compatible attributes.@TimothyMothra this will be part of next release (this week I believe)
Yes, that should be fine I think. Just needs API approval.
We have the exact same concern as OP and just hit this problem today when investigating adding our first source-generated logs to an application: we wanted to have identifiers in the logs match the same identifiers we already use in traces so we can cross query and correlate the data in Datadog, but the
[LoggerMessage]generator does not allow us to use the same notation.Reading this thread here, Iām actually very surprised that so many people seemed to be unaware of the standard naming convention defined by OpenTelemetry which uses dots for nested properties and underscores for spaces. We are stuck with using mismatching identifiers right now.
This suggestion from @JWilh is what makes the most sense to me as well:
This would also be similar to how model binding works in the Web API space. Take for example the
[FromHeader]binding attribute, and this example:Here we have the same type of limitation, where the characters allowed for C# identifiers are not sufficient to map against characters allowed in the http header, so an explicit override is provided.
Using an attribute and constant interpolation support would also permit this, which is a bit convoluted but would still work:
@mwierzchowski can we close this one now? I am seeing the PR is merged.
Realistic ETA is March release. @dariusclay please do your best to push it thru api reviews.
@geeknoid do you know when can mark
TagNameattribute as stable and remove the experimental marking from it? Azure monitors want to use it.CC @TimothyMothra
@tarekgh We have a list of constants defined like this:
Our tags follow the standard for OpenTelemetry and we use the āconst User.Idā tag in activities/traces and in logs either directly or in a logger scope. And itās working fine with LoggerMessage.Define, it just donāt work with LoggerMessageAttribute. In case we would now switch, we would break all of our already created queries and dashboards or we would get a mismatch between our traces and logs, which would create not required confusion for our operations guys.
We donāt want that, so we are not able to use the LoggerMessageAttribute, but need to stick with LoggerMessage.Define, which is from my point of view a drawback of the LoggerMessageAttribute, that shouldnāt be required, as itās way more convenient than LoggerMessage.Define.
Hi @tarekgh,
We use the OpenTelemetry library to send our logs to NewRelic. The OpenTelemetry library and also other 3 party libraries like MassTransit submits traces with the properties organized like this: http.method http.status_code, ⦠(or for masstransit messaging.masstransit.destination_address and so on)
That is the reason we also adapted this for our own logs and traces and because we already have a large set of properties defined that we use for our traces and logs we donāt want to change that.
Yes, Iām aware that C# doesnāt support parameter names with dots, but maybe itās possible to think about supporting it like by just ignoring the dots when matching the parameters:
Or as another solution that would than support basically everything maybe an additional attribute?
With that people are always free to do what they want and the feature would support everything and people would always have a workaround š
What are you thoughts about this š? Thanks in advance
Just FYI, https://github.com/dotnet/runtime/pull/79038