Use ASP. NET Core middleware Diagnostics, corediagnostics

Source: Internet
Author: User

Use ASP. NET Core middleware Diagnostics, corediagnostics

ASP. NET Core Middleware (Middleware) Diagnostics is used. For introduction to Middleware, see the previous article ASP. NET Core development-Middleware (Middleware ).

Diagnostics middleware is mainly used to report and handle exceptions and error messages in ASP. NET Core, and diagnose Entity Framework Core migration errors.

There are several other functions, including the welcome page, error code page, and page 404. As well as a fairly good Log Viewing function, this function is also required by many people to directly view logs online.

The following describes the functions.

Create an ASP. NET Core Web Application project-select no authentication for the Web Application.

The created Project automatically references the Diagnostics middleware.

NuGet reference:

Install-Package Microsoft.AspNetCore.Diagnostics

Report exceptions and error messages

The project created by default is automatically added to the Configure method in Startup.

            if (env.IsDevelopment())            {                app.UseDeveloperExceptionPage();                app.UseBrowserLink();            }            else            {                app.UseExceptionHandler("/Home/Error");            }

This is the report and handling of exception information.

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

If you want to keep displaying the error message, place app. usemediaexceptionpage (); outside if. Or after the release, you can change 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 EF Core information.

Welcome Page

Add: app. UseWelcomePage ();

The following page is displayed. You can specify the corresponding page by using the extension method.

 

HTTP Error code page

The default program contains errors such as 404 or 500. The page is not displayed and only the corresponding code is returned.

Add the following method to Configure in Startup. cs: app. UseStatusCodePages ();

The page that does not exist is displayed as follows.

 

 

The following is a simple custom error message.

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

You can also use

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

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

You can also use

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

The difference between the two is redirection and execution.

Log view Elm

Log Viewing is also a function of Diagnostics middleware. It is also convenient to use.

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

Install the package and open Startup. cs. First, add the package to the Service in the ConfigureServices method.

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

The log filtering level is Debug.

Then add the Configure method

            app.UseElmPage();            app.UseElmCapture();

 

App. UseElmPage (); specifies the log display page, app. UseElmCapture (); records logs.

Run the program to access http: // localhost: 5000/Elm to view some information records.

 

There are still some problems with this component. We can make some modifications to better meet the actual situation.

Official GitHub: https://github.com/aspnet/Diagnostics

 

If you think this article is helpful to you, click"Recommendation", Thank you.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.