Skip to content
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

Exceptions do not contain the rendered message #201

Open
cmeeren opened this issue Jun 23, 2022 · 4 comments
Open

Exceptions do not contain the rendered message #201

cmeeren opened this issue Jun 23, 2022 · 4 comments
Labels

Comments

@cmeeren
Copy link

cmeeren commented Jun 23, 2022

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.

@cmeeren cmeeren added the bug label Jun 23, 2022
@DavidCMulder
Copy link

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
Copy link
Author

cmeeren commented Aug 6, 2022

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
Copy link
Author

cmeeren commented Aug 6, 2022

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants