使用记录方法执行时间.NET注释



是否有任何现有的库可以只记录方法执行时间(我更喜欢serilog,anotar.serilog(和方法注释?

如果您的意图是记录控制器/操作的运行时间。您可以为此编写自己的中间件。检查以下示例:

public class DiagnosticsMiddleware 
{
private readonly RequestDelegate _next;
public DiagnosticsMiddleware(RequestDelegate next, IElasticClient esClient)
{
_next = next;
_esClient = esClient;
}
public async Task Invoke(HttpContext context)
{ 
Stopwatch sw = Stopwatch.StartNew();

await _next(context);
sw.Stop();
var elapsedTime = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
LogContext.PushProperty("ElapsedTime", elapsedTime);
}
}

你可以像一样配置它

public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<ContextEnricherMiddleware>();
}

最新更新