Log4Net 구성

첫 번째 단계는 Log4Net을 다운로드하는 것입니다.dll
http://download.csdn.net/detail/cpcpc/4011877
2부, 프로필 추가 app.config, 추가 위치는 콘솔 프로그램, Windows 프로그램, Windows 서비스, Asp.Net 웹 사이트, Silverlight
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>


  <log4net>

    <!--1        -->
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <!--        -->
      <file type="log4net.Util.PatternString" value="%  \%date{yyyMMdd}\log.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <!--    -->
        <conversionPattern value="    :%date   ID:[%thread]     :%-5level    :%logger -     :%message%newline" />
      </layout>
      <!--<filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="WARN" />
        <param name="LevelError" value="ERROR" />
        <param name="LevelFatal" value="FATAL" />
        <param name="LevelInfo" value="INFO" />
      </filter>-->
    </appender>

    <!--2            -->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="    :%date   ID:[%thread]     :%-5level    :%logger -     :%message%newline" />
      </layout>
      <!--<filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG" />
        <param name="LevelMax" value="FATAL" />
      </filter>-->
    </appender>

    <!--3     windows   -->
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="    :%date   ID:[%thread]     :%-5level    :%logger -     :%message%newline" />
      </layout>
    </appender>

    <!--4         ,       Access    ,    C  log4net.mdb-->
    <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">
      <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\log4net.mdb" />
      <commandText value="INSERT INTO LogDetails ([LogDate],[Thread],[Level],[Logger],[Message]) VALUES (@logDate, @thread, @logLevel, @logger,@message)" />
      <!--      -->
      <parameter>
        <parameterName value="@logDate" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logLevel" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="240" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
    </appender>

    <!--         ,             。                   。-->
    <root>
      <!--        -->
      <appender-ref ref="LogFileAppender" />
      <!--         -->
      <!--<appender-ref ref="ConsoleAppender" />-->
      <!--Windows    -->
      <!--<appender-ref ref="EventLogAppender" />-->
      <!--             ,           -->
      <!--<appender-ref ref="AdoNetAppender_Access" />-->
    </root>

    <logger name="AppLogging">
      <!--<level value="Info"/>-->
      <level value="All"/>
    </logger>

  </log4net>

</configuration>

세 번째 단계,log4Net을 추가합니다.dll 참조, LogOust 클래스 만들기
public class LogOuts
    {
        public static void Debug(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsDebugEnabled)
            {
                log.Debug(message);
            }
        }

        public static void Error(string message)
        {
            //log4net.ILog log = log4net.LogManager.GetLogger("log4net");
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsErrorEnabled)
            {
                log.Error(message);
            }
        }

        public static void Fatal(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsFatalEnabled)
            {
                log.Fatal(message);
            }

        }

        public static void Info(string message)
        {
            //log4net.ILog log = log4net.LogManager.GetLogger("log4net");
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsInfoEnabled)
            {
                log.Info(message);
            }

        }

        public static void Warn(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            if(log.IsWarnEnabled)
            {
                log.Warn(message);
            }
        }
    }

4단계, LogOuts 클래스가 있는 클래스 라이브러리의Properties 아래의 AssemblyInfo.cs의 추가
WINFORM 응용 프로그램에 [assembly:log4net.Config.DOMConfigurator()] 또는 [assembly:log4net.Config.XmlConfigurator()] WEBFORM에 [assembly:log4net.Config.DOMConfigurator(Config File="web.config", Watch=true)]를 추가할 수 있습니다.
다섯 번째 단계, 호출, 로그 쓰기.로그를 기록해야 하는 곳은 다음과 같이 호출하면 됩니다
LogOuts.Info("로깅 성공!");

좋은 웹페이지 즐겨찾기