.Net 常用ORM框架对比:EF Core、FreeSql、SqlSuger (上篇)
1 public class CalculateExecutionTime 2 { 3 private readonly RequestDelegate _next;//下一个中间件 4 private readonly ILogger _logger; 5 Stopwatch stopwatch; 6 public CalculateExecutionTime(RequestDelegate next, ILoggerFactory loggerFactory) 7 { 8 if (loggerFactory == null) 9 {10 throw new ArgumentNullException(nameof(loggerFactory));11 }12 this._next = next ?? throw new ArgumentNullException(nameof(next));13 _logger = loggerFactory.CreateLogger();14 }15 16 public async Task Invoke(HttpContext context)17 {18 stopwatch = new Stopwatch();19 _logger.LogInformation($@"====接口{context.Request.Path}开始计时====");20 stopwatch.Start();//启动计时器21 await _next.Invoke(context);22 stopwatch.Stop();//停止秒表。23 _logger.LogInformation($@"====接口{context.Request.Path}耗时:{stopwatch.ElapsedMilliseconds}ms====");24 }25 }26 27 public static IApplicationBuilder UseCalculateExecutionTime(this IApplicationBuilder app)28 {29 if (app == null)30 {31 throw new ArgumentNullException(nameof(app));32 }33 return app.UseMiddleware();34 }
2.使用中间件,Configure中加入如下代码
1 app.UseCalculateExecutionTime();//Api计时
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。