I just herited of a very bad Web API code in C#. I need to improve it but I don't know where to start. There are plenty of classes and the code is a nightmare. I also think part of code is never used.
I would like to count the number of time each request is called and what time each request last. I would like to log all my result in a table. In one or two days I could have a clear view of what I the most used and what take time. Client are complaining about that.
I use Nlog for log and a simple Stopwatch to calculation of performance. What do you suggest? I know it's better to use Filter and attribute for traces but how can I log and trace performance like this?
// GET api/Item/5
public Item GetItem(string id)
{
// I don't use string.Format in my real code. This is just for this example.
logger.Trace(string.Format("Request: GET api/Item/{0}", id));
Stopwatch sw = new Stopwatch();
sw.Start();
Item item = context.Items.Single(i => i.Code == id);
sw.Stop();
logger.Trace(string.Format("Response: GET api/values/{0}\r\nElpased={1}\r\rn{2}", id, sw.Elapsed, response));
I found previous information here
Thanks to
http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi
Exact time measurement for performance testing
Of course all these Trace and performance jobs will be activated or deactivated from my config file. For NLog it's built in. For the Stopwatch I plan to build something. I don't plan to keep this alive all the time on production