Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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 f classes and the code is a nightmare.I'm also sure that part of this code is never used.

I would like to count the number of time each request is called and what time each request take. 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?

What I have tried:

Here is a sample of my API:
C#
// 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));


Thanks to

http://weblogs.asp.net/fredriknormen/log-message-request-and-response-in-asp-net-webapi

http://stackoverflow.com/q/969290/196526
Posted
Comments
[no name] 27-Jun-16 4:51am    
How about installing resharper and use Solution Wide Analyzer. You can find lots of help there. Use the test version, it's enough. Sometimes using LINQ is really slow. It nice, only 1 line of code, but sometimes it very2 slow if you compare it using for loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900