Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API Logging and Tracing

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 ?

like image 266
GilliVilla Avatar asked Nov 01 '13 18:11

GilliVilla


Video Answer


1 Answers

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?

  • like image 190
    Tom Florkiewicz Avatar answered Oct 04 '22 04:10

    Tom Florkiewicz