10

Once one has a logging and tracing setup using log4net in place for ASP.NET Web API, what are the specific aspects that need to be logged and/or traced?

I am asking this specifically from Web API perspective. Is there a series of MUST Log this or MUST trace this. Like, INFO traces about a controller's request, any NULL checks, etc.

Is there a reference list that can be validated against to ensure optimum logging and tracing coverage in ASP.NET Web API ?

2
  • 2
    You should log the specifics you need, plus catch any exceptions Commented Nov 1, 2013 at 18:36
  • 1
    There is no "reference list" because "optimum logging and tracing coverage" does not depend on technology (ASP.NET Web API)... it depends on the specific goals you have... Commented Nov 4, 2013 at 15:51

3 Answers 3

12
+200

So I will assume your goal for the logging is, as it should be, to debug the application rather than measure for performance.

While I think there is no one 'right' answer to this, at the minimum I would always try to log

  • Timestamps, class/function names and thread name (if yr running a multithreaded app) in every log line

  • Quick log line @ every entry point, along with the full contents of the passed in request, preferably in a format that would make it easier for you to reissue the same request from fiddler or a similar tool (this can be done via a custom action filter - see how the author uses these for performance monitoring a web api app here )

  • If you are doing any database queries as part of your action, log the full query sql string if possible (once again to make it easier to repeat during debugging) as well as at least the # of rows returned

  • If you are doing any external service calls, log both the full request and response strings

  • Use try/catch blocks to log any exceptions (you can also use something like the ELMAH library to do this automatically rather than handling in code - link here and here

  • Anything that is resolved during runtime - think values from config files, database queries, calculated values, should also be logged

    I'm sure there is more missing from this list - after all this varies on what yr doing in the app. You should be asking yourself at every step of the way - what could go wrong in my application? What things would I or the poor chap that will work on this after me find useful whilst debugging a problem?

  • Sign up to request clarification or add additional context in comments.

    4 Comments

    If there were no holds barred ... what else would be in the list?
    I gave it some thought and past what I listed above, the answer largely depends on what you are doing in the webapi action. Is it calculation intensive? does it access external resources? what sort of data structures or algorithms are involved? if you give me a concrete example of what your application is like, I can give some more potential items to log!
    Its a typical MVC application using WebAPI framework...with the usual controller driven CRUD following the repository pattern. Not calculation intensive but mostly population of Lists, filtering and sorting them and returning them.
    Ah ok so my comment from above applies - log your database connection string if resolved from a config file, all sql query strings as they happen, the query result set counts... Also if you are debugging an asp.net/webapi project locally I highly recommend Glimpse! (getglimpse.com)
    0

    If you are hosting your webapi in IIS, you can enable IIS LOG and counters:

    http://www.iis.net/configreference/system.applicationhost/sites/site/logfile

    Performance Counters:

    http://msdn.microsoft.com/en-us/library/fxk122b4%28v=VS.100%29.aspx

    http://www.iis.net/learn/extensions/configuring-application-request-routing-(arr)/using-performance-counters

    Comments

    0

    An additional category of items to log for a service code that implements authorization authentication and impersonation where elevated access is required. These should be configurable to log either success failure or both.

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.