ASP.NET Core Developer Exception Page

Developer Exception Page contains exception details like 
  • Stack trace including the file name and line number that caused the exception
  • Query String, Cookies and HTTP headers

This developer exception page can be customized to see more specific detail. For example below code customization define a number of line of source code to display before and after the line which causes the exception
DeveloperExceptionPageOptions developerExceptionPageOptions = new DeveloperExceptionPageOptions
{
    SourceCodeLineCount = 5
};
app.UseDeveloperExceptionPage(developerExceptionPageOptions);


How UseDeveloperExceptionPage Middleware works

UseDeveloperExceptionPage Middleware must be plugged into the request processing pipeline as early as possible, so it can handle the exception and display the Developer Exception Page if the subsequent middleware components in the pipeline raise an exception.
Below is sample code with exception middleware
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Run(async (context) =>
    {
        throw new Exception("Some error processing the request");
        await context.Response.WriteAsync("Hello World!");
    });

    if (env.IsDevelopment())
    {
        DeveloperExceptionPageOptions developerExceptionPageOptions = new DeveloperExceptionPageOptions
        {
            SourceCodeLineCount = 5
        };
        app.UseDeveloperExceptionPage(developerExceptionPageOptions);
    }
}


How to set Application Environment

Use the ASPNETCORE_ENVIRONMENT variable to set the application environment. On our local development machine we usually set this environment variable in launchsettings.json file. On a staging or production environment it is set in the operating system. 




Comments