Common Logging & log4net

common logging是一个通用日志接口,log4net是一个更强大的具体实现,另外可用不同的实现,如EntLib的日志、NLog等。

Common.Logging.dll

Common.Logging.Log4Net.dll  (适配器)

 log4net.dll

 

   class  Program
    {
        
// public static ILog log = LogManager.GetCurrentClassLogger();
         public   static  ILog log  =  LogManager.GetLogger( " Program " );
        
static   void  Main( string [] args)
        {
            
// DEBUG < INFO < WARN < ERROR < FATAL

            
if  (log.IsDebugEnabled)
            {
                log.Debug(
" Debug info " );
            }

            log.Info(
" info info " );
            log.Warn(
" Warn info " );
            log.ErrorFormat(
" Error {0} " , " info " );
            log.Fatal(
" fatal info " );
            Console.WriteLine(
" done " );

            Console.ReadLine();
        }
    }

 

 config file

<? 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 >
  
< common >
    
<!-- [DEBUG < INFO < WARN < ERROR < FATAL] -->  
    
< logging >
      
      
<!-- <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
        <arg key="level" value="WARN" />
        <arg key="showLogName" value="true" />
        <arg key="showDataTime" value="true" />
        <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
      </factoryAdapter>
-->

     
     
        
< factoryAdapter  type ="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net" >
          
<!-- FILE
              FILE-WATCH
              INLINE
              EXTERNAL
-->
          
< arg  key ="configType"  value ="INLINE"   />           
          
          
<!-- <arg key="configType" value="FILE-WATCH" />
          <arg key="configFile" value="~/log4net.config" />
-->
        
</ factoryAdapter >     
      
    
</ logging >
  
</ common >
  
  
< log4net >
    
<!-- <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger %ndc - %message%newline" />
      </layout>
    </appender>
-->

    
< appender  name ="RollingLogFileAppender"  type ="log4net.Appender.RollingFileAppender, log4net" >
      
< param  name ="LockingModel"  type ="log4net.Appender.FileAppender+MinimalLock"   />
      
< param  name ="File"  value ="logs/"   />
      
< param  name ="StaticLogFileName"  value ="false"   />
      
< param  name ="DatePattern"  value ="yyyy-MM-dd.'log'"   />
      
< param  name ="AppendToFile"  value ="true"   />
      
< param  name ="MaxSizeRollBackups"  value ="10"   />
      
< param  name ="MaximumFileSize"  value ="5MB"   />
      
< param  name ="RollingStyle"  value ="Date"   />
      
<!-- Size ==- Date -->
      
< layout  type ="log4net.Layout.PatternLayout" >
        
< param  name ="ConversionPattern"  value ="%d [%t] %-5p %c - %m%n"   />
      
</ layout >
    
</ appender >
    
    
< root >
      
< level  value ="DEBUG"   />
      
< appender-ref  ref ="RollingLogFileAppender"   />
    
</ root >
    
    
< logger  name ="MyApp.DataAccessLayer" >
      
< level  value ="DEBUG"   />       
    
</ logger >

    
<!-- root>
        <level value="error" />
        <appender-ref ref="RollingLogFileAppender" />
    </root
-->

    
<!--  ALL DEBUG INFO WARN ERROR FATAL OFF [DEBUG < INFO < WARN < ERROR < FATAL] 大范围 到 小范围 -->
    
< logger  name ="Spring" >
      
< level  value ="ERROR"   />
      
< appender-ref  ref ="RollingLogFileAppender"   />
    
</ logger >

    
< logger  name ="APP"  debug ="false" >
      
< level  value ="Error"   />
      
< appender-ref  ref ="RollingLogFileAppender"   />
    
</ logger >

    
< logger  name ="Game"  debug ="false" >
      
< level  value ="all"   />
      
< appender-ref  ref ="RollingLogFileAppender"   />
    
</ logger >

    
< logger  name ="Web"  debug ="false" >
      
< level  value ="ERROR"   />
      
< appender-ref  ref ="RollingLogFileAppender"   />
    
</ logger >

    
  
</ log4net >
  
</ configuration >

 

 

你可能感兴趣的:(logging)