我使用的是nLog 2.0,在这个有趣的读物之后,我尝试将条件应用于这样一个简单的布局:
layout="${message:when=logger==A}"
layout="${message:when=logger=='A'}"
layout="${message:when='logger==A'}"这些不仅没有任何影响,他们也不会抛出一个错误,所以似乎情况是默默吞没某处(throwExceptions设置为真)。
这是完整的代码,这是一个基本的控制台应用程序。
main.cs:
class Program
{
static void Main( string[] args )
{
NLog.LogManager.GetLogger( "A" ).Info( "from A" );
NLog.LogManager.GetLogger( "B" ).Info( "from B" );
}
}NLog.config (在可执行目录中):
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true">
<targets>
<target name="main" xsi:type="File" fileName="my.log"
deleteOldFileOnStartup="true" keepFileOpen="true"
layout="${callsite} ${message:when=logger=='A'}"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="main" />
</rules>
</nlog>输出:
ConsoleApplication1.Program.Main from A -> this should only log ${callsite}
ConsoleApplication1.Program.Main from B发布于 2011-12-05 12:05:32
谢谢!尝试设置记录器名称:
<logger name="A" minlevel="Trace" writeTo="main" />
<logger name="B" minlevel="Trace" writeTo="main" />现在有两个记录器,它们在代码中是可用的-条件应该可以工作。
https://stackoverflow.com/questions/8383393
复制相似问题