Asp.net core view application status and statistics

Time:2021-8-12

In daily development, we need to pay attention to the resource usage of. Net applications to facilitate troubleshooting and capacity expansion.

Obtain statistical information through Ajax request and display it as a chart, as shown in the following figure:

Clrstats plug-in is a plug-in for statistics of. Net application resource usage, including CPU utilization, GC and thread. It supports obtaining status information through web requests (access path and authentication can be customized). The data will be returned in JSON format.

GitHub address:https://github.com/itsvse/CLRStats

  • Support. Net framework > = 4.5
  • Support. Net core > = 2.0

install

Clrstats can be obtained from nuget website. You can use the following command to install it:

PM> Install-Package CLRStats

usage

The warehouse samples folder contains examples of asp.net MVC and asp.net core.

ASP.NET MVC

The project needs to create a new owin startup file. The configuration is as follows:

public class Startup
{
	public void Configuration(IAppBuilder app)
	{
		app.UseCLRStatsDashboard();
	}
}

After configuration, enable the project by accessing the site/clrPath, you can get the following information:

{“Server”:{“MachineName”:”DESKTOP-ZH5FQFC”,”SystemDateTime”:”2021/1/24 20:05:44″},”Application”:{“CPU”:{“UsagePercent”:1.171875},”GC”:{“Gen0CollectCount”:0,”Gen1CollectCount”:0,”Gen2CollectCount”:0,”HeapMemory”:60529392,”HeapMemoryFormat”:”57 M”,”IsServerGC”:true},”Thread”:{“AvailableCompletionPortThreads”:1000,”AvailableWorkerThreads”:8190,”UsedCompletionPortThreads”:0,”UsedWorkerThreads”:1,”UsedThreadCount”:39,”MaxCompletionPortThreads”:1000,”MaxWorkerThreads”:8191}}}

ASP.NET Core

In this example, the access path will be customized and the authentication function will be added.

Identity authentication needs to inherit the idashboardauthorizationfilter class and implement the authorize method. The code is as follows:

public class TokenVerification : IDashboardAuthorizationFilter
{
	public bool Authorize(HttpRequest request)
	{
		if (request.Headers.ContainsKey("Token") && request.Headers["Token"].Equals("test"))
		{
			return true;
		}
		return false;
	}
}

Customize the access path and add identity authentication. In the configure method of startup class, add the following configuration:

app.UseCLRStatsDashboard("/custom-link", new DashboardOptions()
{
	Authorization = new IDashboardAuthorizationFilter[] { new TokenVerification() }
});

After configuration, enable the project by accessing the site/custom-linkPath, and the request header needs to carry the token parameter with the value of: test to access successfully.

Use curl tool under Windows system to test. The command is as follows:

curl "http://localhost:4409/custom-link" --header "Token: test"

As shown below:

CLRStats