読者です 読者をやめる 読者になる 読者になる

basyura's blog

あしたになったらほんきだす。

mono で log4net

C#

プロジェクトで log4net を使ってるけど、使ってるだけでどういうものかよく分かってなかったので mono で試してみる。

log4net

ソースをプロジェクトから取得

$ svn checkout http://svn.apache.org/repos/asf/logging/log4net/trunk apache-log4net

$ git svn clone http://svn.apache.org/repos/asf/logging/log4net/trunk apache-log4net

ビルド

$ cd apache-log4net/src/log4net
$ xbuild /p:Configuration=Release log4net.vs2010.sln
$ cd apache-log4net/build/bin/log4net/net/2.0/release
$ ls

log4net.dll

Log4Net.Async

ソースをプロジェクトから取得

ビルド

$ git clone https://github.com/cjbhaines/Log4Net.Async.git
$ cd Log4Net.Async/src
$ xbuild /p:Configuration=Release Log4Net.Async.sln

$ cd Log4Net.Async/src/Log4Net.Async/bin/Release
$ ls

Log4Net.Async.dll

設定ファイルを作る

log4net.config

<log4net>
    <appender name="Console" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%5level %date [%thread] - %message%newline" />
        </layout>
    </appender>
   
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
        <file value="logs/sample.log" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <appendToFile value="true" />
        <maximumFileSize value="100KB" />
        <maxSizeRollBackups value="99" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%5level %date [%thread] - %message%newline" />
        </layout>
    </appender>


    <appender name="asyncRollingFile" type="Log4Net.Async.AsyncRollingFileAppender,Log4Net.Async">
        <file value="logs/sample.log" />
        <!--<lockingModel type="log4net.Appender.FileAppender+InterProcessLock" />-->
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <appendToFile value="true" />
        <maximumFileSize value="100KB" />
        <maxSizeRollBackups value="99" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%5level %date [%thread] - %message%newline" />
        </layout>
    </appender>


    <root>
        <level value="DEBUG" />
        <appender-ref ref="Console" />
    </root>


    <logger name="mylogger">
        <appender-ref ref="RollingFile" />

    </logger>
   
    <logger name="async_mylogger">
        <appender-ref ref="asyncRollingFile" />
    </logger>
</log4net>

サンプルコード

using System;
using log4net;
using log4net.Config;

class Sample
{
     static void Main(string[] args)
     {
          XmlConfigurator.Configure(new System.IO.FileInfo("./log4net.config"));


          ILog logger = LogManager.GetLogger("mylogger");
          logger.Info("log4net : Sample start");
          logger.Info("log4net : Sample end”);


          logger = LogManager.GetLogger("async_mylogger");
          logger.Info("log4net Async : Sample start");
          logger.Info("log4net Async : Sample end");

          logger = LogManager.GetLogger("unknown");
          logger.Info("log4net unknown : Sample start");
          logger.Info("log4net unknown : Sample end");
     }
}

build & run

$ dmcs -reference:log4net.dll,Log4Net.Async.dll sample.cs
$ mono sample.exe

  INFO 2014-08-17 14:08:00,902 [1] - log4net : Sample start
  INFO 2014-08-17 14:08:00,914 [1] - log4net : Sample end
  INFO 2014-08-17 14:08:00,914 [1] - log4net Async : Sample start
  INFO 2014-08-17 14:08:00,915 [1] - log4net Async : Sample end
  INFO 2014-08-17 14:08:00,915 [1] - log4net unknown : Sample start
  INFO 2014-08-17 14:08:00,915 [1] - log4net unknown : Sample end

$ cat logs/sample.log

  INFO 2014-08-17 14:08:00,902 [1] - log4net : Sample start
  INFO 2014-08-17 14:08:00,914 [1] - log4net : Sample end
  INFO 2014-08-17 14:08:00,914 [1] - log4net Async : Sample start
  INFO 2014-08-17 14:08:00,915 [1] - log4net Async : Sample end