在生产环境中,通常有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