-1

Is there any existing library to log the method execution time (I prefer serilog, anotar. serilog) just with method annotation?

arteny
  • 339
  • 1
  • 4
  • 14
  • 2
    https://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 Answers1

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