B
B
bullock2018-02-23 11:10:14
.NET
bullock, 2018-02-23 11:10:14

How to collect time metrics correctly?

The question is related to the fact that this is my first experience of collecting metrics.
Do I correctly collect information about the time the request was executed by the server, for example, I have the following code:

// Один из методов контроллера
public IActionResult SomeMethod()
{
    DateTime metricsStart = DateTime.Now;
    try
    {
        // что то делаю и возвращаю клиенту результат

        return Json( auctions );
    }
    catch ( Exception e )
    {
        logger.LogError( $"error! ip:{accessor.HttpContext.Connection.RemoteIpAddress.ToString()} Discription: {e}." );
        return Json( "error" );
    }
    finally
    {
        string timeDifference= ( DateTime.Now - metricsStart ).TotalMilliseconds.ToString();
        logger.LogInformation( $"Ok. ip:  {accessor.HttpContext.Connection.RemoteIpAddress.ToString()} Time: {timeDifference}" );
    }
}

This is fine?
UPDATE
Is it possible to do this in production?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2018-02-23
@bullock

To calculate the time, you should use the Stopwatch class from the System.Diagnostics space

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question