Spring.NETのロギング設定
Spring.NETのロギング設定についてメモ
Spring.NETではロギングに「log4net」やEnterprise Libraryの「Logging Application Block」なんかを使えるんだけど、それらを直接使うんじゃなくて「Common.Logging」というロギングライブラリを挟んで、実際のロガーを隠蔽している。
で、その設定がちょっとややこしいので書いておく。
まずはロギングを行うクラスを定義する。ロガーにはCommon.Logging.ILogというインターフェースからアクセスできる。
Hoge.vb
Imports Common.Logging Public Class Hoge Private _logger As ILog Public Property Logger() As ILog Get Return _logger End Get Set(ByVal value As ILog) _logger = value End Set End Property Sub Run() Logger.Info("Start Run method.") ' Do something Logger.Info("End Run method.") End Sub End Class
アプリケーション構成ファイルにSpring.NETの設定とCommon.Loggingの設定、ついでにオブジェクト定義*1も追加しておく。
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" /> </sectionGroup> </configSections> <spring> <context> <resource uri="config://spring/objects" /> </context> <objects xmlns="http://www.springframework.net"> <object name="logger" type="Spring.Objects.Factory.Config.LogFactoryObject, Spring.Core"> <property name="LogName" value="sampleLogger" /> </object> <object name="hoge" type="SpringLoggingSample.Hoge, SpringLoggingSample" singleton="true"> <property name="Logger" ref="logger" /> </object> </objects> </spring> <common> <logging> <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net"> <arg key="configType" value="FILE" /> <arg key="configFile" value="log4net.config" /> </factoryAdapter> </logging> </common> </configuration>
今回は「log4net」を使うので、「factoryAdapter」にCommon.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Netを指定している。
この部分をCommon.Logging.EntLib.EntLibLoggerFactoryAdapter, Common.Logging.EntLibに変えるとロガーを「Logging Application Block」に切り替える事ができる。
「log4net」の設定は別ファイルに書いておくので、configFileプロパティには「log4net.config」というファイル名を指定しておく。
<common> <logging> <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net"> <arg key="configType" value="FILE" /> <arg key="configFile" value="log4net.config" /> </factoryAdapter> </logging> </common>
以下が実際のロガーの設定。ファイルとコンソールに出力する。
log4net.config
<?xml version="1.0" encoding="utf-8" ?> <log4net> <!-- ファイルへのログ出力 --> <appender name="rollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <file value="Log\" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd.LOG" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-5level - %message%newline" /> </layout> </appender> <!-- コンソールへのログ出力 --> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-5level - %message%newline" /> </layout> </appender> <logger name="sampleLogger"> <level value="DEBUG" /> <appender-ref ref="rollingLogFileAppender" /> <appender-ref ref="ConsoleAppender" /> </logger> </log4net>
で、最後にエントリーポイント。アプリケーションコンテキストを取ってきて、「hoge」オブジェクトを取りだし、Runメソッドを呼び出すだけ。
Program.vb
Imports Spring.Context Module Program Sub Main() Dim context As IApplicationContext = ConfigurationManager.GetSection("spring/context") Dim obj As Hoge = context.GetObject("hoge") obj.Run() Console.ReadKey() End Sub End Module
実行すると以下のように表示される。
*1:普段は別ファイルに分離するけど、面倒くさいのでくっつけておいた