nblumhardt / serilog-sinks-browserhttp

Serilog sink for client-side Blazor, with matching ASP.NET Core server relay
https://serilog.net
Apache License 2.0
52 stars 22 forks source link

Exceptions showing up as type TextException #24

Open JasonLooney opened 10 months ago

JasonLooney commented 10 months ago

We're using these libraries to send log messages from a Blazor WebAssembly app up to an ASP.NET server in Azure, which in turn logs to ApplicationInsights. However, all exceptions coming from the client are showing up as the type TextException in the logs and are missing the exception details.

How can we fix this?

On the client, our logger is configured like so:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.ControlledBy(levelSwitch)
    .Enrich.WithProperty("InstanceId", Guid.NewGuid().ToString("n"))
    .WriteTo.BrowserHttp(controlLevelSwitch: levelSwitch, endpointUrl: $"{builder.HostEnvironment.BaseAddress}ingest")
    .CreateLogger();

And on the server:

builder.Host.UseSerilog((context, services, configuration) => configuration
    .ReadFrom.Configuration(context.Configuration)
    .ReadFrom.Services(services)
    .Enrich.FromLogContext()
    .WriteTo.ApplicationInsights(new TelemetryConfiguration { ConnectionString = appInsightsConnection }, TelemetryConverter.Traces));

// ...

webApp.UseSerilogIngestion();
webApp.UseSerilogRequestLogging();
nblumhardt commented 10 months ago

Hi Jason,

TextException is this class from Serilog.Formatting.Compact.Reader.

On the client side, exceptions are captured using ToString(), and the result ends up wrapped in a TextException server-side.

The only way you should see TextException appear anywhere is some code going to the exception's Type directly; most Serilog sinks etc. won't do that, and instead use ToString(), which for these exceptions will show the original type/stack trace/message.

My guess is there's something in the App Insights sink responsible for this; if you spot it and need help figuring out a workaround, let me know.

Hope this helps, Nick