ASP.NET Web API Logging and Tracing

2019-03-15 14:08发布

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 ?

3条回答
对你真心纯属浪费
2楼-- · 2019-03-15 14:57

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?

  • 查看更多
    狗以群分
    4楼-- · 2019-03-15 15:12

    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.

    查看更多
    登录 后发表回答