Log4net

-- IT Trend 2011. 9. 1. 16:54
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Log4Net에 대해서 정리를 하려다가 귀차니즘으로 인해 아래 포스트로 대신한다.
.Net계열에서 사용할 수 있는데 VS6과 같은 환경에서 쓸 수 있는 건 없나?


Introduction

This article describes the basic steps to be followed for using Log4net in a web application. Log4net is an open source library that allows .NET applications to log statements to a variety of targets.

Background

This article assume you have basic working knowledge of C#, and ASP.NET programming.

Using the code

Log4net is very simple to use yet powerful logging option. This article describes log4net usage in a web application in six easy steps.

  • Step 1: Get the latest version of log4net library and add reference of it in your project.

  • Step 2: Add below line in your AssemblyInfo.cs file.
    [assembly: log4net.Config.XmlConfigurator(ConfigFile="Web.config", Watch=true)]   //For log4net 1.2.10.0

    Above statement provides the information about config file in which log4net configuration parameters are defined.

  • Step 3: Add below section in Web.Config file.

    <configSections>
    <sectionname="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/
    >
    </configSections
    >
    <log4netdebug="true">
    <appendername="RollingLogFileAppender"type="log4net.Appender.RollingFileAppender"
    >
    <filevalue="C:\\TestProj\\TestLog.txt"/>
    <appendToFilevalue="true"/>
    <rollingStylevalue="Size"/>
    <maxSizeRollBackupsvalue="10"/>
    <maximumFileSizevalue="10MB"/>
    <staticLogFileNamevalue="true"/>
    <layouttype="log4net.Layout.PatternLayout">
    <conversionPatternvalue="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n"/>
    </layout>
    </appender>
    <root>
    <levelvalue="DEBUG"/>
    <appender-refref="RollingLogFileAppender"/>
    </root
    >
    </log4net>

    Above section defines the configuration parameters to be used for logging.

    RollingLogFileAppender describes the appender to be used for logging. This means that the log should be written in a file, which will rollover when full. There are several other appender available and more than one appender can be attached to a logger.

    The layout is responsible for formatting the logging request, whereas an appender takes care of sending the formatted output to its destination. Layout used above would format the output as below:
    2006-07-14 20:26:04,033 [1736] ERROR Utility [PayStub] - Could not find a part of the path "c:\inetpub\wwwroot\TestProj\Template\PayStub.xml"

  • Step 4: If you want log4net to add its own diagnostics messages then add below lines in web.config file.
    <appSettings>
    <addkey="log4net.Internal.Debug"value="true"/
    >
    </appSettings>
    Add below lines after system.web section:
    <system.diagnostics>
    <traceautoflush="true">
    <listeners>
    <addname="textWriterTraceListener"
    type="System.Diagnostics.TextWriterTraceListener"
    initializeData="C:\\TestProj\\TestProjlog4net.txt"/>
    </listeners>
    </trace
    >
    </system.diagnostics>

  • Step 5: Now follow below steps in your code-behind.

    a. Add namespace

    using log4net;

    b. Add below declarations within class definition

    privatestatic readonly ILog log = LogManager.GetLogger(typeof(TestPage1).Name);
    OR
    privatestaticreadonly ILog log = LogManager.GetLogger(  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  • Step 6: Now you are ready for logging. Use below statements to log your messages in log file:
    if (log.IsErrorEnabled)
    {
    log.Error("Page Load failed : " + ex.Message);
    }
    OR
    if (log.IsDebugEnabled)
    {
    log.Debug("Application loaded successfully.");
    }
    We have just scratched the surface of log4net. There are several other good features available in log4net and it can be used to collect vital information for several purposes. Happy Programming.

  • 출처 : http://www.codeproject.com/KB/aspnet/log4net.aspx

    '-- IT Trend' 카테고리의 다른 글

    Hibernate  (0) 2011.09.22
    QRCode  (0) 2011.09.05
    Smart Work  (0) 2011.06.29
    N-Screen  (0) 2011.03.29
    Cloud Computing  (0) 2011.03.21
    posted by 어린왕자악꿍