正确记录 [英] Logging done right

查看:82
本文介绍了正确记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是关于日志记录,以及如何处理可能影响代码和运行时行为的日志语句.

So my problem is about logging, and how to handle the log statements which can have influence on your code and your runtime behavior.

日志文件...每个程序都应编写相应的文件以解决问题,但是正确的做法呢?

Log files... every program should write those for propper problem solving but how to do it right?

获取大多数日志语句非常昂贵,因为它们应该提供有用的信息,并且即使完全禁用日志记录也始终可以构建它们.

The most log-statements are verry expensive to get because they should provide usefull information and they are always built even the logging is disabled totaly.

可以通过xmls,inCode或某些设置等配置日志记录,但这不能解决字符串构建问题.

The logging can be configured by xmls, inCode or in some settings etc. but this doesn't solve the string building problem.

例如,以下代码始终加载一棵巨大的延迟加载树,在正常执行期间永远不会完全加载该树.

For example the following code is always loading a huge lazy loaded tree which would never be loaded totaly during normal execution.

仅创建此日志记录语句的整个树,以显示在日志文件中

The whole tree is only created for this logging statement to be displayed in the log file

(好吧,我知道...,但这只是大多数程序中存在的所有复杂日志记录方法的替代品,这些程序在正常发行版执行期间不应该执行)

public void SomeMethod(){
    logger.Debug(someObject.GetHeavyDescriptionFromLazyTree());
}

即使关闭了日志记录,也会始终调用方法someObject.GetHeavyDescriptionFromLazyTree(),因此对于此问题,有一些常见的解决方案,例如:

The Method someObject.GetHeavyDescriptionFromLazyTree() is always invoked even if logging is turned off, so there are some common solutions for this problem like:

public void SomeMethod(){
#if DEBUG
    logger.Debug(someObject.GetHeavyDescriptionFromLazyTree());
#endif       
}

public void SomeMethod(){
    if(logger.DoLogDebug())
        logger.Debug(someObject.GetHeavyDescriptionFromLazyTree());
}

我认为很明显,#if DEBUG编译器标志不是高效代码的解决方案. 如果我使用logger.DoLogDebug(),将会有过多的代码来检查是否启用了日志记录...因此也不是解决方案.

I think it's clear that the #if DEBUG compiler flag is no solution for productive code. If I use the logger.DoLogDebug() there would be excessive code for checking if the logging is enabled... so this can't be the solution either.

我认为ConditionalAttribute可以提供帮助,但它也与编译器标志绑定,并且不会停用对GetHeavyDescriptionFromLazyTree的调用

I thought the ConditionalAttribute could help but it's also bound to the compiler flags and it doesn't deactivate the call to GetHeavyDescriptionFromLazyTree

[Conditional("DEBUG")]  
public void Debug(string debugMessage) 
{ 
    Console.WriteLine(debugMessage); 
} 

所以我正在寻找一种日志记录解决方案

So I'm looking for a logging solution which

  • 使用调试语句不会过度破坏我的源代码
  • 在运行时而不是在编译时确定日志记录级别的地方
  • 仅在必要时解析语句(logLevel> definedLogLevel)

额外的日志行以获得最佳答案;-)

Extra loglines for the best answer ;-)

我正在寻找.NET 2.0中的解决方案

Edited: I'm looking for a solution in .NET 2.0

推荐答案

惰性日志记录"的一种方法是使用表达式(创建返回消息的函数委托),而不是使用直接求值:

One approach to 'lazy logging' is to use an expression (to create a function delegate that returns the message) rather than using direct evaluation:

logger.Debug(() => someObject.GetHeavyDescriptionFromLazyTree());

这有其自身的缺点(委托的构造),但是它可能比昂贵的方法便宜.通常,您会将其作为重载提供,并且仅在您知道评估表达式的开销很大的情况下使用它.

This has its own downsides (construction of the delegate), but it can be cheaper than the expensive method. Typically you would provide this as an overload and only use it in the case where you know evaluating the expression is expensive.

这篇关于正确记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆