背景
在开发一个webapi项目,使用了Swagger首页展示Http接口。为了测试、提升接口性能,现在对接口加入分析工具。
MiniProfiler是一款很好的轻量级性能分析工具,提供库和UI。通过它可以查看程序的时间消耗,提供程序调试和性能优化帮助。
MiniProfiler is a library and UI for profiling your application. By letting you see where your time is spent, which queries are run, and any other custom timings you want to add, MiniProfiler helps you debug issues and optimize performance.
1、安装Nuget包
Install-Package MiniProfiler.AspNetCore.Mvc
2、注册和启用MiniProfiler
public void ConfigureServices(IServiceCollection services)
{
/*
* 其他代码
*/
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1.0",
Title = "GDWC.Core.API",
Description = "说明文档",
TermsOfService = null,
Contact = new OpenApiContact
{
Name = "GDWC.Core",
Email = "",
Url = new Uri("http://www.whfhjc.com.cn/"),
}
});
services.AddMiniProfiler(option =>
{
option.RouteBasePath = "/profiler";
//(option.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(10);
});
/*
* 其他代码

本文介绍了如何在.NET Core WebApi项目中集成Swagger和MiniProfiler,以实现接口性能分析和测试。首先,通过安装Nuget包引入MiniProfiler。接着,确保其在`UseEndpoints`之前注册并启用。然后,自定义Swagger首页`index.html`,并将MiniProfiler的Tag标签插入其中。当遇到MiniProfiler的Tag为空时,检查注册顺序。最后,展示了如何在代码中使用MiniProfiler进行性能分析。
最低0.47元/天 解锁文章
595

被折叠的 条评论
为什么被折叠?



