2
2

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導入・設定方法

Posted at

①nugetでlog4netインストール

②AssemblyInfo.csにLog4net設定値の保存場所を設定

\Properties\AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

→Web.configからLog4net設定値がよみこまれる

③Web.configにLog4net設定値記載
log4netの設定を セクションに書くことを宣言しておきます。

Web.config
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

ログの出力形態(どこに出力するか)を記述します。

Web.config
  <log4net>
    <!-- ファイルアペンダ(ローテーションあり) -->
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="C:\weblogger\log_upload."/>"/>
      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="10"/>
      <param name="MaximumFileSize" value="5MB"/>
      <param name="RollingStyle" value="date"/>
      <param name="StaticLogFileName" value="false"/>
      <param name="DatePattern" value="&quot;.&quot;yyyyMMdd&quot;.log&quot;"/>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %5p (%C:%M:%L) - %m%n"/>
      </layout>
    </appender>

    <root>
      <priority value="INFO"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>

以下の通り記述するとエラーレベル毎に出力ファイル名が分けられる。

Web.config
  <log4net>
    <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="C:\weblogger\log_debug."/>
      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="10"/>
      <param name="MaximumFileSize" value="5MB"/>
      <param name="RollingStyle" value="date"/>
      <param name="StaticLogFileName" value="false"/>
      <param name="DatePattern" value="&quot;.&quot;yyyyMMdd&quot;.log&quot;"/>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %5p (%C:%M:%L) - %m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="DEBUG" />
      </filter>
    </appender>
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="C:\weblogger\log_info."/>
      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="10"/>
      <param name="MaximumFileSize" value="5MB"/>
      <param name="RollingStyle" value="date"/>
      <param name="StaticLogFileName" value="false"/>
      <param name="DatePattern" value="&quot;.&quot;yyyyMMdd&quot;.log&quot;"/>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %5p (%C:%M:%L) - %m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
    </appender>
    <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="C:\weblogger\log_error."/>
      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="10"/>
      <param name="MaximumFileSize" value="5MB"/>
      <param name="RollingStyle" value="date"/>
      <param name="StaticLogFileName" value="false"/>
      <param name="DatePattern" value="&quot;.&quot;yyyyMMdd&quot;.log&quot;"/>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %5p (%C:%M:%L) - %m%n"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="ERROR" />
      </filter>
    </appender>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="DebugAppender" />
      <appender-ref ref="InfoAppender" />
      <appender-ref ref="ErrorAppender" />
    </root>
  </log4net>

出力レベルを変更するときはlevel value="DEBUG"の設定値を変更する。

2
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?