私はnLog2.0を使用しており、この興味深い記事を読んだ後、次のような単純なレイアウトに条件を適用しようとしました。
layout="${message:when=logger==A}"
layout="${message:when=logger=='A'}"
layout="${message:when='logger==A'}"
これらはいずれも効果がないだけでなく、エラーもスローしないため、条件がどこかで静かに飲み込まれているように見えます(throwExceptions
trueに設定されています)
- 条件付きレイアウトを機能させる方法は?彼らも働きますか
- 何かが間違っている/認識されていない場合、nLogは例外をスローできますか?
これが完全なコードです。これは基本的なコンソールアプリケーションです。
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