2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

log4netのConfig

Posted at

久々にC#を使って、LOGを出力する際にいろいろ調べたのがめんどくさかったので
自分なりに使いやすいLOG4NET用のコンフィグを残しておく。

アセンブリに関する情報

AssemblyInfo.csに以下を追記。
一番下でOK

[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Log4net.Config.xml", Watch = true)]

Configの実際の内容

以下の内容のLog4net.Config.xmlを作成

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>

    <!-- 通常ログ -->
    <appender name="InfoLogAppender" type="log4net.Appender.RollingFileAppender">
      <File value=".\\Logs\\Info_" />
      <!-- ファイル名は日付ごと -->
      <param name="DatePattern" value='yyyyMMdd".log"' />

      <param name="RollingStyle" value="date" />
      <param name="StaticLogFileName" value="false" />
      <param name="MaximumFileSize" value="10MB" />
      <param name="MaxSizeRollBackups" value="10" />

      <param name="AppendToFile" value="true" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMax" value="WARN" />
        <param name="LevelMin" value="INFO" />
      </filter>

      <layout type="log4net.Layout.PatternLayout">
        <ConversionPattern value="%date [%thread] [%-5level] %class %method (L%line) - %message%n" />
      </layout>
    </appender>

    <!-- エラーログ -->
    <appender name="ErrorLogAppender" type="log4net.Appender.RollingFileAppender">
      <File value=".\\Logs\\Error_" />
      <!-- ファイル名は日付ごと -->
      <param name="DatePattern" value='yyyyMMdd".log"' />

      <param name="RollingStyle" value="date" />
      <param name="StaticLogFileName" value="false" />

      <param name="AppendToFile" value="true" />

      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMax" value="FATAL" />
        <param name="LevelMin" value="ERROR" />
      </filter>

      <param name="MaximumFileSize" value="10MB" />
      <param name="MaxSizeRollBackups" value="10" />

      <layout type="log4net.Layout.PatternLayout">
        <ConversionPattern value="%date [%thread] [%-5level] %class %method (L%line) - %message%n" />
      </layout>
    </appender>

    <!-- デバッグ用:分割ファイル出力 -->
    <appender name="DebugLogAppender" type="log4net.Appender.RollingFileAppender">
      <File value=".\\Logs\\Trace_" />
      <!-- ファイル名は日付ごと -->
      <param name="DatePattern" value='yyyyMMdd".log"' />

      <!-- ファイル名は日付ごと -->
      <param name="RollingStyle" value="date" />
      <param name="StaticLogFileName" value="false" />

      <param name="AppendToFile" value="true" />

      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMax" value="FATAL" /> 
        <param name="LevelMin" value="TRACE" />
      </filter>

      <param name="MaximumFileSize" value="10MB" />
      <param name="MaxSizeRollBackups" value="10" />

      <layout type="log4net.Layout.PatternLayout">
        <ConversionPattern value="%date [%thread] [%-5level] %class %method (L%line) - %message%n" />
      </layout>
    </appender>
    
    <!-- コンソール -->
    <appender name="ColoredConsole"  type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="FATAL" />
        <foreColor value="White" />
        <backColor value="Red" />
      </mapping>
      <mapping>
        <level value="ERROR" />
        <foreColor value="White" />
        <backColor value="Purple" />
      </mapping>
      <mapping>
        <level value="WARN" />
        <foreColor value="Purple" />
        <backColor value="White" />
      </mapping>
      <mapping>
        <level value="INFO" />
        <foreColor value="White" />
        <backColor value="Blue" />
      </mapping>
      <mapping>
        <level value="DEBUG" />
        <foreColor value="White" />
        <backColor value="Green" />
      </mapping>

      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d[%t] %p - %m%n"/>
      </layout>
    </appender>

    <root>
      <!-- TRACE以上のログを記録 -->
      <level value="TRACE" />
      <!-- 使用する Appender -->
      <appender-ref ref="InfoLogAppender" />
      <appender-ref ref="ErrorLogAppender" />
      <appender-ref ref="DebugLogAppender" />
      <appender-ref ref="ColoredConsole" />
    </root>

  </log4net>
</configuration>
2
5
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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?