はじめに
よく忘れちゃうので、明日の自分のためにメモっとく。
環境
- Windows7 Professional SP1
- Microsoft Visual Studio Version 12.0.31101.00 Update 4
- Microsoft .NET Framework Version 4.5.51209
- Microsoft Visual C# 2013
- NLog 3.1.0.0
Example
Program.cs
/// <summary>
/// 【備忘録】NLog 出力するファイルを分ける
/// </summary>
class NLog01
{
private static NLog.Logger logger1 = NLog.LogManager.GetLogger("fooLogger");
private static NLog.Logger logger2 = NLog.LogManager.GetLogger("barLogger");
static void Main(string[] args)
{
logger1.Info("fooLoggerに出力");
logger2.Info("barLoggerに出力");
}
}
NLog.config
<targets>
<target
name="fooFile"
xsi:type="File"
layout="${longdate} ${uppercase:${level}} ${message}"
fileName="${basedir}/logs/${shortdate}.log"
archiveAboveSize ="5000000"
maxArchiveFiles="2"/>
<target
name="barFile"
xsi:type="File"
layout="${longdate} ${uppercase:${level}} ${message}"
fileName="${basedir}/logs/${longdate}.log"
archiveAboveSize ="5000000"
maxArchiveFiles="2"/>
</targets>
<rules>
<logger name="fooLogger" minlevel="Trace" writeTo="fooFile" />
<logger name="barLogger" minlevel="Trace" writeTo="barFile" />
</rules>