I have my ASP.NET Core 9 Web API app using custom ExceptionHandlerMiddleware Middleware to handle all exceptions as shown below:
public sealed class ExceptionHandlerMiddleware(IWebHostEnvironment env, ILogger<ExceptionHandlerMiddleware> logger) : IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
try
{
await next(context);
}
catch (DomainException ex)
{
await HandleDomainExceptionAsync(context, ex);
}
catch (DbUpdateException ex)
{
await HandleDbUpdateExceptionAsync(context, ex);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private Task HandleDomainExceptionAsync(HttpContext context, DomainException exception)
{
logger.LogCritical(exception, exception.Message);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
string result = JsonSerializer.Serialize(Envelope.Error(exception.Message), options);
context.Response.ContentType = MediaTypeNames.Application.Json;
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return context.Response.WriteAsync(result);
}
private Task HandleDbUpdateExceptionAsync(HttpContext context, Exception exception)
{
logger.LogCritical(exception, exception.Message);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
string result = JsonSerializer.Serialize(Envelope.Error("Error occurred while saving data. Please try again later."), options);
context.Response.ContentType = MediaTypeNames.Application.Json;
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return context.Response.WriteAsync(result);
}
private Task HandleExceptionAsync(HttpContext context, Exception exception)
{
logger.LogCritical(exception, exception.Message);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
string result = env.IsLocal() ? JsonSerializer.Serialize(Envelope.Error(exception.Message), options) : JsonSerializer.Serialize(Envelope.Error("An unexpected fault happened. Try again later."), options);
context.Response.ContentType = MediaTypeNames.Application.Json;
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return context.Response.WriteAsync(result);
}
}
Now, I came across ProblemDetails and looks like ASP.NET Core 9 has support for it.
All I need to do is to,
Program.cs:
builder.Services.AddProblemDetails();
var app = builder.Build();
app.UseExceptionHandler();
app.Run();
The setup is easy and straight forward. But the problem is whenever I test with exception I get the below response.
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.6.1",
"title": "An error occurred while processing your request.",
"status": 500,
"traceId": "00-a2cfefb5c6ca44a142d9810fef0e5f7d-d7103986bf2c06a4-00"
}
Now I need help in understanding how to show StackTrace in lower environments like Local, Offline, Development. I'm not able to find an option to configure StackTrace based on environment.
I checked the problem details docs and not able to find any information.



