달력

32024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

log4net 사용

.NET/ASP.NET 2006. 3. 27. 14:33

1. log4net 사용방법.

 

 프로그램 다운로드
http://logging.apache.org/log4net/downloads.html 에서 다운로드


2. 압축을 풀고, bin\net\1.1\release 디렉토리에서 log4net.dll 파일을
프로젝트 bin 폴더에 복사한다.

3. Web.config 파일에 log4net Section 등록.

-----------------------------------------------------------------------------------------------------------------------------------------------
  <configSections>

   <!-- Log4net Section -->
   
   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
 

  </configSections>

-----------------------------------------------------------------------------------------------------------------------------------------------

configSections 가 존재할 경우 <section /> 부분만 기존 섹션에 추가한다.

아래는 elmah 로깅 서비스와 같이 존재할 경우임.
-----------------------------------------------------------------------------------------------------------------------------------------------
  <configSections>

    <!-- Allows for a new section group to the Web.config -->

    <sectionGroup name="gotdotnet.elmah">

      <!-- Indicates that inside the section group there will be an errorLog section -->

      <section
          name="errorLog"
          type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

      <!-- Indicates that inside the section group there will be an errorMail section -->

      <section
          name="errorMail"
          type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

    </sectionGroup>

    <!-- Log4net Section -->
   
   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
 

  </configSections>
  -----------------------------------------------------------------------------------------------------------------------------------------------


4. log4net 섹션 세부 설정.
  -----------------------------------------------------------------------------------------------------------------------------------------------
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="d:\\WebDevel\\CloseBetaLog\CloseBeta.log" />
      <param name="AppendToFile" value="true" />
      <param name="DatePattern" value="-yyyy-MM-dd" />
      <param name="RollingStyle" value="Date" />
      <param name="StaticLogFileName" value="true" />

      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
      </layout>
    </appender>

    <!-- root 카테고리를 설정하고 appender들을 추가한다. 기본우선순위 (default priority) 역시 설정된다. -->
    <root>
      <priority value="WARN" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>

  </log4net>
  -----------------------------------------------------------------------------------------------------------------------------------------------

5. Global.asax 의 Application_Start 메서드에서 log4net 을 초기화 시킨다.

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
       
        // log4net Configure
        log4net.Config.DOMConfigurator.Configure();
    }


6. 사용방법.


클래스를 하나 생성.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using log4net;

public partial class LoggingTest : System.Web.UI.Page
{
    private static ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


    protected void Page_Load(object sender, EventArgs e)
    {
        LoggingTest.logger.Error(System.DateTime.Now + " : ???④릿 濡?洹??????);
       
    }
}


7. 로그 확인.

d:\WebDevel\CloseBetaLog\CloseBeta.log 를 열어보면 아래와 같이 남는다.

2006-03-27 14:15:26,053 [3536] ERROR LoggingTest [(null)] - 2006-03-27 오후 2:15:26 : 에 남긴 로그입니다
2006-03-27 14:15:26,084 [3536] ERROR LoggingTest [(null)] - 2006-03-27 오후 2:15:26 : 에 남긴 로그입니다
2006-03-27 14:15:26,115 [3536] ERROR LoggingTest [(null)] - 2006-03-27 오후 2:15:26 : 에 남긴 로그입니다
2006-03-27 14:15:26,147 [3536] ERROR LoggingTest [(null)] - 2006-03-27 오후 2:15:26 : 에 남긴 로그입니다
2006-03-27 14:15:26,178 [3536] ERROR LoggingTest [(null)] - 2006-03-27 오후 2:15:26 : 에 남긴 로그입니다
2006-03-27 14:15:26,209 [3536] ERROR LoggingTest [(null)] - 2006-03-27 오후 2:15:26 : 에 남긴 로그입니다
2006-03-27 14:18:48,584 [2400] ERROR LoggingTest [(null)] - 2006-03-27 오후 2:18:48 : 에 남긴 로그입니다


8. 윈도우 Tail 유틸리티로 로그 꼬리만 계속 보기.

http://ophilipp.free.fr/op_tail.htm  에서 다운로드 후 로그 파일을 지정한 후 start 하면 됨.


 

Posted by tornado
|