LoginSignup
21
26

More than 5 years have passed since last update.

【備忘録】NLog 出力するファイルを分ける

Last updated at Posted at 2015-02-28

はじめに

よく忘れちゃうので、明日の自分のためにメモっとく。

環境

  • 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>
21
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
26