2010. 4. 19. 16:35

dll 참조를 한다. (설치경로 참조)
 - Common.Logging.dll 1.2.0.0
 - Common.Logging.Log4Net.dll 1.2.0.2
 - log4net.dll 1.2.10.0

logging adaper에는 4가지의 로그를 설정할 수 있다.
 - NoOpLoggerFactoryAdapter : 설정없이 기본으로 사용된다.
 - ConsoleOutLoggerFactoryAdapter : console창에 로그가 나타난다.
 - TraceLoggerFactoryAdapter : System.Diagnostics.Trace for logging output 과 동일하다.
 - Log4NetLoggerFactoryAdapter : 

app.config 파일에서 아래와 같이 설정이 가능하다.

▲ configSection 설정
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler,              Common.Logging" />
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
   ...
</configuration>


▲ Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging 설정방식
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections ...>
   ...
    <common>
      <logging>
        <factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging">
          <arg key="level" value="DEBUG" />
          <arg key="showLogName" value="true" />
          <arg key="showDataTime" value="true" />
          <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
        </factoryAdapter>
      </logging>
    </common>
   ...
</configuration>

▲ Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging 설정방식
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections ...>
   ...
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
        <arg key="level" value="DEBUG" />
        <arg key="showLogName" value="true" />
        <arg key="showDataTime" value="true" />
        <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
      </factoryAdapter>
    </logging>
  </common>
   ...
</configuration>

▲ Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net 설정방식

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections ...>
   ...
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <!-- choices are INLINE, FILE, FILE-WATCH, EXTERNAL-->
        <!-- otherwise BasicConfigurer.Configure is used   -->
        <!-- log4net configuration file is specified with key configFile-->
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>
   ...
  <log4net debug="false">
    <root>
      <!-- TRACE, DEBUG, ERROR, FATAL, INFO, WARN -->
      <level value="ERROR"/>
      <appender-ref ref="RollingLogFileAppender_Piggy"/>
    </root>

    <logger name="HelloApp.Program">
      <level value="ERROR" />
    </logger>

    <logger name="Spring">
      <level value="INFO" />
    </logger>

    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <param name="File" value="Logs\Application.log.txt"/>
      <param name="datePattern" value="MM-dd HH:mm"/>
      <param name="AppendToFile" value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>                         </layout>
    </appender>
    <appender name="HttpTraceAppender" type="log4net.Appender.ASPNetTraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
      </layout>
    </appender>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
      </layout>
    </appender>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Logs\Log.txt"/>
      <param name="AppendToFile" value="true"/>
      <param name="MaxSizeRollBackups" value="10"/>
      <param name="MaximumFileSize" value="5MB"/>
      <param name="RollingStyle" value="Size"/>
      <param name="StaticLogFileName" value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
      </layout>
    </appender>
    <appender name="RollingLogFileAppender_Piggy"                                                      
                    type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Logs\ExceptionLog"/>
      <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="yyyyMMdd&quot;.log&quot;"/>
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
      </layout>
    </appender>
  </log4net>
   로그를 찍는방법에는 종류는 4가지가 있다.
        - INLINE
        - FILE
        - FILE-WATCH
        - EXTERNAL

▲  c#에서 사용
using Common.Logging;
.......
ILog Log = LogManager.GetLogger(네임스페이스.클래스명);
Log.Debug("Debug");
Log.Fatal("Fatal");
Log.Error("Error");
Log.Info("Info");
Log.Warn("Warn");

참고
Posted by CoolDragon
2010. 4. 19. 11:55
DI를 하기위한 spring.net에서 제공하는 object 기본 생성 방식은 아래같다.
아래와 같은 방법외에 더 있다면 있는데로 추가하도록 하겠다.

▲ dll 참조추가
 - Spring.Core

▲ configSection 설정
<?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>
  </configSections>
   ...
</configuration>


▲ object 설정

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

  <!-- 1. 같은 config 내에서 object 설정-->
  <spring>
    <context>
      <resource uri="config://spring/objects" />
    </context>
    <objects xmlns="http://www.springframework.net">
      <object name="name" type="namespace.class_name, assembly_name" singleton="true/false" />
      ...
    </objects>

  <!-- 2. 포함리소스 방식 -->
  <spring>
    <context>
      <resource uri="assembly://Spring.IocQuickStart.MovieFinder/
                         Spring.IocQuickStart.MovieFinder/AppContext.xml"/>
    </context>
  </spring>

  <!-- 3. object 별로 xml 분리하여 설정 -->
  <spring>
    <context>
      <resource uri="~/Config/CommonObjects.xml"/>

      <resource uri="~/Config/Production/Services.xml"/>

      <resource uri="file://%PROGRAMFILES%/myapp/ioc.config" />
    </context>
  </spring>

</configuration>

2번으로 할 경우는 필히 잊지말고 관련파일을 해당프로젝트(assembly)에 포함리소스로 변경해주어야 한다.



1,2,3을 혼용해서 사용이 가능한지 모르겠지만 개인적으로 3번처럼 구성하는게 좋아보인다.


▲ config에서 객체생성
  - 기본객체 생성
  - 프로퍼티 형식으로 객체 주입하여 생성
  - constructor의 생성자에 인자로 객체생성 방법

c#
using Spring.Context;
using Spring.Context.Support;
...
IApplicationContext ctx = ContextRegistry.GetContext();
IHello hello = (IHello)ctx.GetObject("MyHello");  // config의 object의 name과 일치
string result = hello.Say("CoolDragon");
Console.WriteLine(result);


Posted by CoolDragon
2010. 4. 19. 11:35
Spring.Net 프레임워크를 사용하기 위해 
app.config 또는 web.config에서 SectionGroup을 선언해주어야 한다.

그리고 이 선언을 통하여 하단부에 SectionGroup에 대한 본문(?)을 생성해야한다.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
     <!-- 어플리케이션에서 사용될 object를 생성하기 위한 설정  -->
    <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>
     <!-- 어플리케이션에서 찍고싶은 로그를 원하는 형태로 설정  -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
...
</configuration>


Posted by CoolDragon