I am working on AngularJS, WebApi. Have angular services to fetch data from database using Entity-Framework LINQ.
So, when I run my app, I want to see how much time my web api took to return response from server?
How can we do it?
I am working on AngularJS, WebApi. Have angular services to fetch data from database using Entity-Framework LINQ.
So, when I run my app, I want to see how much time my web api took to return response from server?
How can we do it?
There are several ways to accomplish this thing.
First way is to use ActinoFilter. For that refer this answer.
Second way is to use Middleware
Add new class file, here it's LoggingMiddleware
public class LoggingMiddleware
{
private readonly RequestDelegate _next;
LoggingDataAccess _loggingDataAccess; // your data access
readonly Stopwatch _stopwatch = new Stopwatch();
public LoggingMiddleware(RequestDelegate next, LoggingDataAccess loggingDataAccess)
{
_next = next;
_loggingDataAccess = loggingDataAccess;
}
public async Task Invoke(HttpContext context)
{
// start request time
_stopwatch.Start();
await _next.Invoke(context);
// end request time and get elapsed milliseconds
_stopwatch.Stop();
// save time to your table
var time= _stopwatch.ElapsedMilliseconds;
//reset stopwatch
_stopwatch.Reset();
}
}
Add your Middleware in Startup.cs
(ASP.NET Core)
public void Configure(IApplicationBuilder app...)
{
app.UseMiddleware<LoggingMiddleware>();
}
Hope this helps!