Skip to content

Logging Exceptions Does Not Include Stack Trace #4959

@Mike-E-angelo

Description

@Mike-E-angelo

Package

Sentry

.NET Flavor

.NET

.NET Version

10.0.103

OS

Any (not platform specific)

OS Version

Windows 11

Development Environment

Visual Studio v18.x

SDK Version

6.1.0

Self-Hosted Sentry Version

No response

Workload Versions

android 36.1.30/10.0.100 VS 18.3.11512.155, VS 18.4.11513.90
ios 26.2.10197/10.0.100 VS 18.3.11512.155, VS 18.4.11513.90
maccatalyst 26.2.10197/10.0.100 VS 18.3.11512.155, VS 18.4.11513.90
maui-windows 10.0.20/10.0.100 VS 18.3.11512.155, VS 18.4.11513.90

UseSentry or SentrySdk.Init call

public sealed class Program
{
	public static void Main(string[] args) => BuildWebApp(args).Run();

	public static WebApplication BuildWebApp(string[] args)
	{
		var builder = WebApplication.CreateBuilder(args);
		builder.Host.UseSerilog((_, c) =>
			                        c.Enrich.FromLogContext()
			                         .MinimumLevel.Information()
			                         // .WriteTo.Console()
			                         // Add Sentry integration with Serilog
			                         // Two levels are used to configure it.
			                         // One sets which log level is minimally required to keep a log message as breadcrumbs
			                         // The other sets the minimum level for messages to be sent out as events to Sentry
			                         .WriteTo.Sentry(s =>
			                                         {
				                                         s.Dsn               = SamplesShared.Dsn;
				                                         s.EnableLogs        = true;
				                                         s.MinimumEventLevel = LogEventLevel.Information;
			                                         }));

		// builder.WebHost.UseSentry(SamplesShared.Dsn);

		// The App:
		var webApplication = builder.Build();
		Configure(webApplication);

		return webApplication;
	}

	static void Configure(IApplicationBuilder app)
	{
		// An example ASP.NET Core middleware that throws an
		// exception when serving a request to path: /throw
		app.Use(async (context, next) =>
		        {
			        var log = context.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger<Program>();

			        log.LogInformation("Handling some request...");

			        try
			        {
				        throw new InvalidOperationException("This is a test");
			        }
			        catch (Exception e)
			        {
				        log.LogInformation(e, "An exception was encountered");
			        }

			        // Sends an event which includes the info and debug messages above
			        Log.Logger.Error("Logging using static Serilog directly also goes to Sentry.");

			        if (context.Request.Path == "/greetings")
			        {
				        context.Response.ContentType = "text/plain";
				        await context.Response.WriteAsync("Hello, World!");
			        }
			        else if (context.Request.Path == "/throw")
			        {
				        var hub = context.RequestServices.GetRequiredService<IHub>();
				        hub.ConfigureScope(s =>
				                           {
					                           // More data can be added to the scope like this:
					                           s.SetTag("Sample", "ASP.NET Core"); // indexed by Sentry
					                           s.SetExtra("Extra!", "Some extra information");
				                           });

				        // Logging through the ASP.NET Core `ILogger` while using Serilog
				        log.LogInformation("Logging info...");
				        log.LogWarning("Logging some warning!");

				        // The following exception will be captured by the SDK and the event
				        // will include the Log messages and any custom scope modifications
				        // as exemplified above.
				        throw new Exception("An exception thrown from the ASP.NET Core pipeline");
			        }
			        else
			        {
				        await next();
			        }
		        });
	}
}

Steps to Reproduce

Please see the above log.LogInformation(e, "An exception was encountered");

An exception is being reported to the ASP.NET Core ILogger while using Serilog, but the stack trace is not emitted when viewed in explore/logs:

Image

Expected Result

A stack trace is emitted, or else, why is the exception being reported? If a stack trace cannot be displayed, a link to an error for more information should be presented to the user for further details.

Actual Result

No stack trace reported, nor is a link to an error for more information:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Waiting for: Product Owner

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions