当最低级别为“调试"时,为什么不记录“信息"级别的消息? [英] Why don't Info level messages get logged when minimum level is Debug?

查看:98
本文介绍了当最低级别为“调试"时,为什么不记录“信息"级别的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Nlog的C#应用​​程序中有以下代码.带有调试"的消息将被记录,但没有信息".我假设由于app.config中的minLevel设置为"Debug",因此"Info"消息也将被记录,因为Debug的优先级高于"Info".但是它们不会被记录.我在哪里错了?

I have the following code in my C# application using Nlog. The messages with 'Debug' get logged, but 'Info' do not. I had assumed that since minLevel is set to 'Debug' in app.config, the 'Info' messages will also get logged, since Debug has higher priority over 'Info'. But they do not get logged. Where am i wrong ?

谢谢.

if (logger.IsDebugEnabled) logger.Debug(logMessage) else if (logger.IsInfoEnabled)log.Info(logMessage);

这是app.config设置

This is the app.config setting

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<targets>
  <target name="file" xsi:type="File" fileName="E:/Logoutputs/Remissol-Nlog.txt" />
</targets>

<rules>
  <logger name="*" minlevel="Debug" writeTo="file" />
</rules>

推荐答案

这与@Nobby的答案并不完全不同,因为他对如何提高水平是正确的.我会说,您不必在登录前进行IfXXXXEnabled检查.

This isn't really a different answer than @Nobby, because he is right about how to the levels. I will say that you don't have to do the IfXXXXEnabled check before logging.

这是您当前在登录呼叫站点的问题中所用的C#代码中的内容:

This is what you currently have in your C# code in your question for your logging call site:

if (logger.IsDebugEnabled) logger.Debug(logMessage) else if (logger.IsInfoEnabled)log.Info(logMessage); 

@Nobby建议,通过执行以下操作可以使此操作更简洁:

@Nobby suggests, this could be made cleaner by doing this:

if (logger.IsDebugEnabled) logger.Debug(logMessage);
if (logger.IsInfoEnabled) logger.Info(logMessage); 

我认为那不是真的.使用您的原始代码,将仅记录调试消息或信息消息.如果删除嵌套,则根据启用的日志记录级别,可能会同时记录两条消息.

I don't think that is really true. With your original code, ONLY the Debug message OR the Info message will be logged. If you remove the nesting, then, depending on the logging level that is enabled, you could get BOTH messages logged.

我认为您应该以这种方式记录日志:

I think you should be logging this way:

logger.Debug(logMessage);
logger.Info(logMessage); 

日志记录方法(logger.Debug,logger.Info等)已经进行了IsXXXEnabled检查,并且如果未启用该日志记录级别,则不会进行日志记录.您可以通过直接拨打记录调用而不是使用if检查对其进行保护来保存键入(和混乱)记录.

The logging methods (logger.Debug, logger.Info, etc) already do an IsXXXEnabled check and won't log if that logging level is not enabled. You can save a log of typing (and confusion) by just making the logging call directly rather than protecting it with the if check.

有些时候您想使用IsXXXEnabled检查.例如,如果您需要做一些工作来计算要记录的值,并且只想在进行记录时进行该计算:

There are times when you want to use the IsXXXEnabled check. Such as if you have some work to do to calculate the value(s) to be logged and you only want do that calculation if you are logging:

if (logger.IsDebugEnabled)
{
  int value1 = DoSomeExpensiveCalculation();
  int value2 = DoSomeOtherExpensiveCalculation();
  logger.DebugFormat("v1 = {0}, v2 = {1}", value1, value2);
}

这样,除非您实际要记录它们,否则您无需支付计算的价格.

This way you are not paying the price of the calculations unless you are actually going to log them.

最后,在NLog 2.0中,您可以访问lambda语法,该语法将允许您推迟可能昂贵的操作,除非实际记录了该消息:

Finally, in NLog 2.0 you have access to lambda syntax that would allow you to defer potentially expensive operations unless the message is actually logged:

logger.Debug(() => string.Format("v1 = {0}, v2 = {1}", DoSomeExpensiveCalculation(), DoSomeOtherExpensiveCalculation()));

logger.Debug(() => "message" + i + ", " + j + "," + k);

在两种情况下,除非启用了DEBUG级别,否则不会评估传递给NLog的表达式.

In both cases, the expression that is being passed to NLog will not be evaluated unless the DEBUG level is enabled.

这篇关于当最低级别为“调试"时,为什么不记录“信息"级别的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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