EFCore记录慢查询日志

在生产环境中,通常有DBA同事对数据库进行监控,在发现如慢查询等问题时反馈给开发团队进行解决。

.NET平台提供了诊断机制,借助该机制可以实现EFCore记录慢查询日志功能,这样开发团队就可以通过日志告警发现慢查询问题而无需被动依赖DBA同事的反馈。

记录慢查询日志

基于.NET6创建API项目,安装WJChi.Net.EFCoreSlowQuery包,示例代码如下:

using Api.Database;
using EFCoreExtensions.Middlewares;
using Microsoft.EntityFrameworkCore;
​
var builder = WebApplication.CreateBuilder(args);
​
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContext<InfoDbContext>(opt =>
{
    opt.UseSqlServer("Server = localhost;Database = Demo;User ID = sa;Password = Docker2022!;Application Name = EFCore;");
});
​
​
var app = builder.Build();
​
// Configure the HTTP request pipeline.
// Configuration via code
app.UseEFCoreSlowQuery(opt =>
{
    opt.ServiceName = "Demo APIs";
    opt.SlowQueryThresholdMilliseconds = 20;
});
app.MapControllers();
​
app.Run();

也支持通过配置文件进行配置:

builder.Services.Configure<EFCoreSlowQueryOptions>(builder.Configuration.GetSection(EFCoreSlowQueryOptions.OptionsName));
app.UseEFCoreSlowQuery();

配置文件内容如下:

{
    "EFCoreSlowQuery": {
        "ServiceName": "Demo APIs",
        "SlowQueryThresholdMilliseconds": 20
    }
}

 

输出如下:

 

 

 

点击这里可以查看完整示例。

推荐阅读

How to identify slow running queries in SQL Server

Overview of Logging and Interception

DiagnosticSource User's Guide

posted @ 2022-02-04 21:20  雪飞鸿  阅读(242)  评论(0编辑  收藏  举报