Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Diagnostics Middleware in ASP.NET Core

Shulou Source: shulou.com Published: 2022-06-02 06:44:41 09月19日 Update

It is believed that many inexperienced people have no idea about how to use Diagnostics middleware in ASP.NET Core. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Report exception and error messages

Projects created by default will automatically add this paragraph to the Configure method in Startup

If (env.IsDevelopment ()) {app.UseDeveloperExceptionPage (); app.UseBrowserLink ();}

Else {app.UseExceptionHandler ("/ Home/Error");}

This is the reporting and handling of exception information.

A detailed error message app.UseDeveloperExceptionPage () is displayed in the development environment; in a non-development environment, jump to the app.UseExceptionHandler ("/ Home/Error"); / Home/Error page.

Put app.UseDeveloperExceptionPage (); outside the if if you want to keep displaying error messages. Or after the release, set the corresponding system environment to Development to display the error message.

EF Core is used in the project. You can add app.UseDatabaseErrorPage (); to display information about EF Core.

Welcome page

Add the Configure method in Startup.cs: app.UseWelcomePage ()

The following page will be displayed when you start the program. There is an extension method to specify the corresponding page.

HTTP error code page

Related errors such as 404 or 500 in the default program will not display the page, only the corresponding code will be returned.

Also added to the Configure method in Startup.cs: app.UseStatusCodePages ()

Visiting a page that does not exist will be displayed as follows.

Here is a simple custom error message

App.UseStatusCodePages ("text/plain", "Error, status code: {0}\ r LineZero")

You can also use the

App.UseStatusCodePagesWithRedirects ("~ / errors/ {0}"); / / relative root path

App.UseStatusCodePagesWithRedirects ("/ base/errors/ {0}"); / / absolute path

You can also use the

App.UseStatusCodePagesWithReExecute ("/ error/http {0}")

The difference between the above two is jump and execution.

Log view Elm

Log viewing is also a function of Diagnostics middleware. It's also convenient to use.

You need to add an additional Microsoft.AspNetCore.Diagnostics.Elm package.

After installing the package, open the Startup.cs and first join the service in the ConfigureServices method.

Services.AddElm (elmOptions = > {elmOptions.Filter = (loggerName, loglevel) = > loglevel = = LogLevel.Debug;})

Filter log level is Debug

Then add the Configure method.

App.UseElmPage (); app.UseElmCapture ()

App.UseElmPage (); displays the page for the specified log, app.UseElmCapture (); logs.

By running the program to access http://localhost:5000/Elm, you can view some information records.

After reading the above, have you mastered how to use Diagnostics middleware in ASP.NET Core? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Information method error log page middleware environment program code content report more path problem project development helpless for this function reason Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn Microsoft NVidia Shulou Technology