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)

Most upvoted comments

@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.

    [LoggerMessage(LogLevel.Error, RequestReadErrorMessage)]
    public static partial void RequestReadError(
        ILogger logger,
        Exception exception,
        [TagName(HttpClientLoggingTagNames.Method)] HttpMethod method,
        [TagName(HttpClientLoggingTagNames.Host)] string? host,
        [TagName(HttpClientLoggingTagNames.Path)] string? path);

@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:

Or as another solution that would than support basically everything maybe an additional attribute?

[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, 
    [LoggerArgument("messaging.id")] string id, 
    [LoggerArgument("messaging.type")] string type);

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:

public IActionResult Get([FromHeader(Name = "X-Custom-Header")] string customHeader)
{
    ...
}

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:

[LoggerMessage(Level = LogLevel.Information, EventName = "LogMessage",
    Message = $"The event {{{OpenTelemetryAttributes.Messagging.Id}}} of type {{{OpenTelemetryAttributes.Messagging.Type}}} did something")]
public static partial void LogMyMessage(this ILogger logger, 
    [LoggerArgument(OpenTelemetryAttributes.Messagging.Id)] string id, 
    [LoggerArgument(OpenTelemetryAttributes.Messagging.Type)] string type);

@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 TagName attribute as stable and remove the experimental marking from it? Azure monitors want to use it.

@tarekgh We have a list of constants defined like this:

public static class Tags
{
    public static class User
    {
        public const string Id = "user.id";

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,

@JWilh can’t you sanitize the parameter names and use something different than .? The issue is the source generator can generate structs with the fields named the same as the parameters. Using . is not going to work. Also, C# doesn’t allow using . in the parameter’s names. The names inside the message templates and the parameter names must match. Any reason you cannot use underscores in the message template too? support that inside the source generator can add untrivial complexity.

Why can’t you sanitize the parameter names in your code? like using underscore for example.

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:

[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 messagingid, string messagingtype);

Or as another solution that would than support basically everything maybe an additional attribute?

[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, 
        [LoggerArgument("messaging.id")] string id, 
        [LoggerArgument("messaging.type")] string type);

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

I can’t currently use the source generator with its lack of @ destructuring support;

Just FYI, https://github.com/dotnet/runtime/pull/79038