Reputation: 2557
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 ?
Thanks.
if (logger.IsDebugEnabled) logger.Debug(logMessage) else if (logger.IsInfoEnabled)log.Info(logMessage);
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>
Upvotes: 1
Views: 1900
Reputation: 27608
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.
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 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);
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.
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.
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);
In both cases, the expression that is being passed to NLog will not be evaluated unless the DEBUG level is enabled.
Upvotes: 3
Reputation: 121
Your problem lies with the nested if statements in your example. Both Debug and Info log levels are enabled according to your config file. The first if statement is then evaluated and its block executed because the condition is true. The else block is therefore never reached and your Info message is never logged.
I suggest that you split your nested if statements into 2 separate statements if you want to log the message for Debug and Info levels.
Upvotes: 2