Is there any existing library to log the method execution time (I prefer serilog, anotar. serilog) just with method annotation?
Asked
Active
Viewed 1,661 times
-1
-
2https://github.com/Fody/MethodTimer / https://github.com/Fody/Anotar – mjwills Dec 04 '18 at 11:53
-
@Selvin and other, this question already has answered but the topic of those questions is different, it is not related annotations what is the point of my question – arteny Dec 08 '18 at 18:01
1 Answers
0
If your intention is to log elapsed time of the Controller/Action. You can write your own Middleware for this purpose. check the example below:
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);
}
}
and you can configure it like
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<ContextEnricherMiddleware>();
}

Derviş Kayımbaşıoğlu
- 28,492
- 4
- 50
- 72
-
There is no mention of the OP actually using ASP.NET. As posted, you can log the execution time of a controller call in ASP.NET, it cannot log the execution time of an arbitrary method. – nvoigt Dec 04 '18 at 13:32
-