简体   繁体   English

登录多层应用程序

[英]Logging in Multi-Tier Application

Im working in an Enterprise Project, and im trying to implement good logging using nLog , i was wondering if there is any book talking about best practice for Logging, 我正在企业项目中工作,并尝试使用nLog实施良好的日志记录,我想知道是否有任何书籍在谈论有关日志记录的最佳实践,

as i dont wanna start to log everything and anything without a standard pattern, it would be realy helpful if i can start from where others stoped, 因为我不想开始记录没有标准模式的所有内容,所以如果我可以从其他人停止的地方开始,那将非常有帮助,

or maybe if someone can provide me info on best practice using nLog.... 也许有人可以使用nLog向我提供最佳实践的信息。...

thanks in advance... 提前致谢...

Think hard about everything you think you need to log. 认真考虑您认为需要记录的所有内容。 In my experience I found that most of the lines that most developers log are in fact errors that should have been thrown as exceptions. 根据我的经验,我发现大多数开发人员记录的大多数行实际上都是应该作为异常抛出的错误。 This often results in flooded logging databases (or mail boxes) that nobody looks at and nobody trusts. 这通常会导致日志记录数据库(或邮箱)泛滥成灾,没人关注,也没人信任。 In the applications I write, I hardly ever log (and catch) anything (besides of course logging exceptions that bubble up to the top of the call stack). 在我编写的应用程序中,我几乎从不记录(并捕获)任何内容(当然,除了记录到调用堆栈顶部的异常)。

The few log lines that are left should be written with a clear (and verbose) message that clearly indicate what's happening. 剩下的几行日志应写有清晰(且冗长)的消息,以明确指示正在发生的事情。 When you do this you will hardly need to specify a 'logger per type', a feature that most logging frameworks need. 执行此操作时,几乎不需要指定“每种类型的记录器”,这是大多数记录框架所需的功能。 Loggers per type are used to prevent log events from certain types or parts of the system to be processed. 每种类型的记录器用于防止来自某些类型或待处理系统部分的日志事件。 However, when you follow the pattern of "log little, throw often", you will find out that you don't need to have a logger per type. 但是,当您遵循“少量记录,经常抛出”的模式时,您会发现不需要每种类型都有记录器。

Here is one question from here on SO that is trying to get some logging best practices documented: Logging best practices 这是关于SO的一个问题,它试图记录一些最佳日志记录最佳实践: 最佳日志记录

Some common themes: 一些常见的主题:

Probably the most common logging practice is to retrieve a logger in each type, based on that type , like this: 可能最常见的日志记录做法是根据该类型检索每种类型的记录器 ,如下所示:

class MyClass
{ 
  private static readonly Logger logger = LogManager.GetCurrentClassLogger();

  public void DoSomething()
  {
    logger.Info("Doing something");
  }
}

This you allows you a great degree of flexibility in configuring and controlling your logging. 这使您在配置和控制日志记录方面具有极大的灵活性。 If your namespaces are logically laid out, you can easily turn logging on or off or set it to a particular level for a given class, namespace, part of a namespace, etc. 如果您的命名空间在逻辑上进行了布局,则可以轻松地打开或关闭登录,或者将其设置为给定类,命名空间,命名空间的一部分等的特定级别。

If you wrap the logger, be careful and wrap it correctly. 如果包装记录仪,请小心并正确包装。 See this post for some (not necessarily definitive) discussion about logging abstractions. 有关日志抽象的一些(不一定是确定的)讨论,请参见本文 Some important issues to consider when wrapping: Do you want to maintain call site info? 包装时要考虑的一些重要问题:是否要维护呼叫站点信息? Think twice about making a single static logger that does not give you the ability to configure logging differently for different areas of your code. 三思而后行,使单个静态记录器无法为您的代码的不同区域配置不同的日志记录。

Hope this helps. 希望这可以帮助。

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

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