serilog-contrib / serilog-sinks-applicationinsights

A Serilog sink that writes events to Microsoft Azure Application Insights
Apache License 2.0
220 stars 71 forks source link

Exceptions do not contain the rendered message #201

Open cmeeren opened 2 years ago

cmeeren commented 2 years ago

Description

When logging exceptions, the sink sends the message template, but not the actual rendered message.

Reproduction

Config:

loggerConfig.WriteTo.ApplicationInsights(
  services.GetRequiredService<TelemetryConfiguration>(),
  TelemetryConverter.Traces,
  restrictedToMinimumLevel = LogEventLevel.Debug)

Log statement:

Log.Error(exn("Test exception"), "Starting retry {Count} of {MaxRetries}", 1, 3)

The following is sent to AI:

{
    "name": "AppExceptions",
    "time": "2022-06-23T06:56:19.0654782Z",
    "tags": {
        "ai.application.ver": "0.17.0.26",
        "ai.cloud.role": "...",
        "ai.cloud.roleInstance": "...",
        "ai.operation.id": "385ca7fcecd58e424739a5ca2412ec72",
        "ai.operation.parentId": "448bf2ce8d3ddec4",
        "ai.operation.name": "...",
        "ai.internal.sdkVersion": "dotnetc:2.20.0-103",
        "ai.internal.nodeName": "..."
    },
    "data": {
        "baseType": "ExceptionData",
        "baseData": {
            "ver": 2,
            "exceptions": [
                {
                    "id": 56885004,
                    "outerId": 0,
                    "typeName": "System.Exception",
                    "message": "Test exception",
                    "hasFullStack": true
                }
            ],
            "severityLevel": "Error",
            "properties": {
                "AspNetCoreEnvironment": "Development",
                "MaxRetries": "3",
                "_MS.ProcessedByMetricExtractors": "(Name:\u0027Exceptions\u0027, Ver:\u00271.1\u0027)",
                "DeveloperMode": "true",
                "Count": "1",
                "MessageTemplate": "Starting retry {Count} of {MaxRetries}"
            }
        }
    }
}

Expected behavior

Just like traces, the data send for exceptions contains the actual rendered log message (above, Starting retry 1 of 3) so that it can be displayed in AI.

If there is no standard field for this, a custom property (e.g. RenderedMessage, or another suitable name) is fine with me and would solve all my problems.

Relevant package, tooling and runtime versions

Using Serilog.Sinks.ApplicationInsights 4.0.0 on .NET 6.

DavidCMulder commented 2 years ago

Hi Chris, I think you can quite easily adjust the TraceTelemetryConverter like:

internal class CustomTraceTelemetryConverter: TraceTelemetryConverter
{
    public override void ForwardPropertiesToTelemetryProperties(LogEvent logEvent, ISupportProperties telemetryProperties, IFormatProvider formatProvider)
    {
        base.ForwardPropertiesToTelemetryProperties(
            logEvent, 
            telemetryProperties, 
            formatProvider, 
            includeLogLevel: false,
            includeRenderedMessage: true,
            includeMessageTemplate: true);
    }
}

and then

.WriteTo.ApplicationInsights(
                    telemetryConfiguration,
                    new CustomTraceTelemetryConverter());

should do the trick.

cmeeren commented 2 years ago

Thanks! Would it be possible for you to include it by default? I think it makes sense to always include the rendered message (for simpler search and display in AI), and as I understand it, it's a non-breaking change.

cmeeren commented 2 years ago

Also, doing this by defaults avoids the caveat that with your workaround, I rely on the implementation detail that TelemetryConverter.Traces is a TraceTelemetryConverter. (Statically it is typed to ITelemetryConverter, and the implementing type could change without my knowing it. I would then not pick up those changes if I use my own converter.)

DavidCMulder commented 2 years ago

Hi @cmeeren . Actually I am just a passerby, that had the same issue as you. I think the current default behavior is probably because of the way ExceptionTelemetry is handled in the Microsoft.ApplicationInsights sdk. There they have some logic to parse the Message out of the Exceptions.