简体   繁体   English

ASP.NET Web API日志记录和跟踪

[英]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? 一旦使用适用于ASP.NET Web API的log4net进行日志记录和跟踪设置,需要记录和/或跟踪的具体方面是什么?

I am asking this specifically from Web API perspective. 我从Web API的角度来看这个问题。 Is there a series of MUST Log this or MUST trace this . 是否有一系列必须记录这个必须跟踪这个 Like, INFO traces about a controller's request, any NULL checks, etc. 比如,INFO跟踪控制器的请求,任何NULL检查等。

Is there a reference list that can be validated against to ensure optimum logging and tracing coverage in ASP.NET Web API ? 是否有可以验证的引用列表,以确保ASP.NET Web API中的最佳日志记录和跟踪覆盖率?

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 ) 每个入口点的快速日志行@以及传入请求的完整内容,最好采用一种格式,使您更容易从fiddler或类似工具重新发出相同的请求(这可以通过自定义操作完成)过滤器 - 请参阅作者如何使用这些来监控web api应用程序的性能

  • 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 如果您正在执行任何数据库查询作为操作的一部分,请尽可能记录完整查询sql字符串(再次使其在调试期间更容易重复)以及至少返回的#行

  • 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 使用try / catch块记录任何异常(您也可以使用类似ELMAH库的东西自动执行此操作而不是代码处理 - 链接此处此处

  • 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? 在调试问题后,我或者在我之后会对此有用的什么事情会发现有用吗?

  • 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. 这些应该是可配置的,以记录成功失败或两者。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM