Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include filename and line number in stacktrace from ASP.NET Core

In .NET full framework, it is possible to include the filename and line number in a stacktrace generated from a release build, by including debug info in the output and setting Debug info to pdb-only or full. When working in ASP.NET Core and Azure Function Apps, these options no longer seem to be accessible through the UI or working if adding the generated XML to the project file. When I create a new project and run it in Release mode, filename and line numbers aren't part of the stacktrace.

How would I include filename and line number on a release build in ASP.NET Core?

Update: Steps to recreate!

Add a new exception logging middleware:

app.Use(async (context, next) =>
{
    try
    {
        await next.Invoke();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
});

Throw an exception in the default generated ValuesController:

[HttpGet]
public IEnumerable<string> Get()
{
    throw new Exception();
}

Build the project (debug/release doesn't matter). Run the application using the exe file and hit /api/values. Stacktrace doesn't contain filename and line numbers:

System.Exception: Exception of type 'System.Exception' was thrown.
   at WebApplication50.Controllers.ValuesController.Get()
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()

(I don't want this to turn into a pros/cons post. I know that some people think that including these information is a death sin and some don't :)

like image 217
ThomasArdal Avatar asked Mar 12 '18 07:03

ThomasArdal


1 Answers

To have stack traces with line numbers:

  • Go to project properties
  • Build
  • Advanced
  • Debugging Information = Full (be sure to do that in Release Configuration)

Doc: https://docs.microsoft.com/en-us/visualstudio/ide/reference/advanced-build-settings-dialog-box-csharp?view=vs-2017#output

(For some weird reasons, I still have some stack traces without line numbers, but that another issue)

like image 148
Tomap Avatar answered Oct 19 '22 16:10

Tomap