• 【log4net】配置文件


    相关资料:

    http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html

    注意:

    //如果为了使得应用程序的配置文件(web/app.config)不至于很臃肿,把log4net的配置单独放置在
    //Log4net.config文件中,那么需要应用程序的AssemblyInfo.cs添加这行代码。目的是程序启动时是找到Log4net.config配置文件。

     

    也可以用代码进行配置:

    请在 应用程序启动前调用:

            private void LoadLog4netConfig()
            {
                string filePath = Server.MapPath("~/Config/Log4net.config");
                FileInfo fileInfo = new FileInfo(filePath);
                //DOMConfigurator已经被XmlConfigurator取代
                //log4net.Config.DOMConfigurator.Configure(fileInfo);
                XmlConfigurator.ConfigureAndWatch(fileInfo);
            }

    注意:

      如果是分层架构中,假设LoadLog4netConfig是在程序集1中调用

    string filePath = Server.MapPath("~/Config/Log4net.config");
    
    
    还是指的是调用了程序集1的应用程序的目录

    ~/Config/Log4net.config
    寻找,

    故,配置文件Log4net.config应该添加在调用log4net的程序集中

    一些代码的用法示例:

            protected void Button1_Click(object sender, EventArgs e)
            {
                //注释掉其他所有的Loger,只用<root>这个父loger
                //再把已经注释掉其他的Loger的<appender-ref ref="LogFileAppender" />
                //复制到<root>,可以同时进行所有子loger向的日志方案
                ILog rootLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                rootLog.Debug("I'm rootLoger");
    
                //ILog logFileAppenderLoggingExample = LogManager.GetLogger("LogFileAppender.LoggingExample");
                //ILog consoleAppLoggingExample = LogManager.GetLogger("ConsoleApp.LoggingExample");
                
                //if (null != logFileAppenderLoggingExample)
                //{
                //    logFileAppenderLoggingExample.Debug("Hello, I'm LogFileAppender.LoggingExample, nothing, just say hello! ");
                //    consoleAppLoggingExample.Debug("Hello, I'm consoleAppLoggingExample, nothing, just say hello! ");
                //}
            }

    Log4net.config文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- 
            .NET application configuration file     
            This file must have the exact same name as your application with .config appended to it. 
            
            For example if your application is ConsoleApp.exe then the config file must be ConsoleApp.exe.config. 
            It must also be in the same directory as the application. 
        -->
    <configuration>
      <!-- Register a section handler for the log4net section -->
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
      </configSections>
      <appSettings>
        <!-- To enable internal log4net logging specify the following appSettings key -->
        <!-- <add key="log4net.Internal.Debug" value="true"/> -->
      </appSettings>
      <!-- This section contains the log4net configuration settings -->
      <log4net>
        <!-- Define some output appenders -->
        <!--Appender的组件来定义输出介质-->
        <!--log4net.Appender.RollingFileAppender日志文件超过maximumFileSize后自动创建第二个文件-->
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
          <file value="rolling-log.txt" />
          <appendToFile value="true" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="100" />
          <rollingStyle value="Size" />
          <staticLogFileName value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <header value="[Header]&#13;&#10;" />
            <footer value="[Footer]&#13;&#10;" />
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
          </layout>
        </appender>
        <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
          <file value="log-file.txt" />
          <!-- Example using environment variables in params -->
          <!-- <file value="${TMP}log-file.txt" /> -->
          <sppendToFile value="true" />
          <!-- An alternate output encoding can be specified -->
          <!-- <encoding value="unicodeFFFE" /> -->
          <layout type="log4net.Layout.PatternLayout">
            <geader value="[Header]&#13;&#10;" />
            <footer value="[Footer]&#13;&#10;" />
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
          </layout>
          <!-- Alternate layout using XML            
                <layout type="log4net.Layout.XMLLayout" /> -->
        </appender>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
          </layout>
        </appender>
        <appender name="NetSendAppender" type="log4net.Appender.NetSendAppender">
          <threshold value="ERROR" />
          <server value="SQUARE" />
          <recipient value="nicko" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
          </layout>
        </appender>
        
        <!-- Example of how to configure the AdoNetAppender    -->    
            <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender" >            
                <connectionString value="Driver={Microsoft Access Driver (*.mdb)};DBQ=C:Workcvs_rootlog4net-1.2access.mdb;UID=;PWD=;" />
                <layout type="log4net.Layout.PatternLayout">                
                    <conversionPattern value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES ('%date','%thread','%level','%logger','%message')" />
                </layout>
            </appender> 
       
            <appender name="A" type="log4net.Appender.ForwardingAppender" >
                <threshold value="WARN"/>            
                <appender-ref ref="ConsoleAppender" />        
            </appender>
        
            <appender name="B" type="log4net.Appender.ForwardingAppender" >
                <filter type="log4net.Filter.LevelRangeFilter">
                    <levelMin value="DEBUG"/>
                    <levelMax value="INFO"/>
                </filter>
                <appender-ref ref="ConsoleAppender" />
            </appender> 
            -->
       <!-- Setup the root category, add the appenders and set the default level -->
       <!-- 在框架的体系里,所有的日志对象都是根日志(root logger)的后代。 
        因此如果一个日志对象没有在配置文件里显式定义,则框架使用根日志中定义的属性。
        在<root>标签里,可以定义level级别值和Appender的列表。如果没有定义LEVEL的值,
        则缺省为DEBUG。可以通过<appender-ref>标签定义日志对象使用的Appender对象。
        <appender-ref>声明了在其他地方定义的Appender对象的一个引用。
        在一个logger对象中的设置会覆盖根日志的设置。
        而对Appender属性来说,子日志对象则会继承父日志对象的Appender列表。
        这种缺省的行为方式也可以通过显式地设定<logger>标签的additivity属性为false而改变。-->
        <root>
          <level value="All" />
          <!--引用的appender的名称-->
          <appender-ref ref="LogFileAppender" />
           <appender-ref ref="ConsoleAppender" />
        </root>
        
        <!-- Specify the level for some specific categories -->
        <!-- 元素预定义了一个具体日志对象的设置。
                然后通过调用LogManager.GetLogger(“testAPP.Logging”)函数,
                你可以检索具有该名字的日志。如果LogManager.GetLogger(…)打开的不是预定义的日志对象,
                则该日志对象会继承根日志对象的属性。知道了这一点,我们可以说,
                其实<logger>标签并不是必须的。-->
        <!--<logger name="ConsoleApp.LoggingExample">
          <level value="ALL" />
          --><!--引用的appender的名称--><!--
          <appender-ref ref="ConsoleAppender" />
        </logger>
        
        <logger name="LogFileAppender.LoggingExample">
          <level value="ALL" />
          --><!--引用的appender的名称--><!--
          <appender-ref ref="LogFileAppender" />
        </logger>-->
      </log4net>
    </configuration>

    官网代码中的Lo4net配置文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- 
            .NET application configuration file     
            This file must have the exact same name as your application with .config appended to it. 
            
            For example if your application is ConsoleApp.exe then the config file must be ConsoleApp.exe.config. 
            It must also be in the same directory as the application. 
        -->
    <configuration>
        <!-- Register a section handler for the log4net section -->
        <configSections>
            <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
        </configSections>
        <appSettings>
            <!-- To enable internal log4net logging specify the following appSettings key -->
            <!-- <add key="log4net.Internal.Debug" value="true"/> -->
        </appSettings>
        <!-- This section contains the log4net configuration settings -->
        <log4net>
            <!-- Define some output appenders -->
            <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
                <file value="rolling-log.txt" />
                <appendToFile value="true" />
                <maxSizeRollBackups value="10" />
                <maximumFileSize value="100" />
                <rollingStyle value="Size" />
                <staticLogFileName value="true" />
                <layout type="log4net.Layout.PatternLayout">
                    <header value="[Header]&#13;&#10;" />
                    <footer value="[Footer]&#13;&#10;" />
                    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
                </layout>
            </appender>
            <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
                <file value="log-file.txt" />
                <!-- Example using environment variables in params -->
                <!-- <file value="${TMP}log-file.txt" /> -->
                <sppendToFile value="true" />
                <!-- An alternate output encoding can be specified -->
                <!-- <encoding value="unicodeFFFE" /> -->
                <layout type="log4net.Layout.PatternLayout">
                    <geader value="[Header]&#13;&#10;" />
                    <footer value="[Footer]&#13;&#10;" />
                    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
                </layout>
                <!-- Alternate layout using XML            
                <layout type="log4net.Layout.XMLLayout" /> -->
            </appender>
            <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; - %message%newline" />
                </layout>
            </appender>
            <appender name="NetSendAppender" type="log4net.Appender.NetSendAppender">
                <threshold value="ERROR" />
                <server value="SQUARE" />
                <recipient value="nicko" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
                </layout>
            </appender>
            <!-- Example of how to configure the AdoNetAppender        
            <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender" >            
                <connectionString value="Driver={Microsoft Access Driver (*.mdb)};DBQ=C:Workcvs_rootlog4net-1.2access.mdb;UID=;PWD=;" />
                <layout type="log4net.Layout.PatternLayout">                
                    <conversionPattern value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES ('%date','%thread','%level','%logger','%message')" />
                </layout>
            </appender> 
            -->
            <!-- 
            <appender name="A" type="log4net.Appender.ForwardingAppender" >
                <threshold value="WARN"/>            
                <appender-ref ref="ConsoleAppender" />        
            </appender>
            <appender name="B" type="log4net.Appender.ForwardingAppender" >
                <filter type="log4net.Filter.LevelRangeFilter">
                    <levelMin value="DEBUG"/>
                    <levelMax value="INFO"/>
                </filter>
                <appender-ref ref="ConsoleAppender" />
            </appender> 
            -->
            <!-- Example of how to configure the AdoNetAppender to connect to MS Access -->
            <appender name="ADONetAppender_Access" type="log4net.Appender.AdoNetAppender">
                <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Workcvs_rootlog4net-1.2access.mdb;User Id=;Password=;" />
                <commandText" value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
                <parameter>
                    <parameterName value="@log_date" />
                    <dbType value="String" />
                    <size value="255" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%date" />
                    </param>
                </parameter>
                <parameter>
                    <parameterName value="@thread" />
                    <dbType value="String" />
                    <size value="255" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%thread" />
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@log_level" />
                    <dbType value="String" />
                    <size value="50" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%level" />
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@logger" />
                    <dbType value="String" />
                    <size value="255" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%logger" />
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@message" />
                    <dbType value="String" />
                    <size value="1024" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%message" />
                    </layout>
                </parameter>
            </appender>
            <!-- Example of how to configure the AdoNetAppender to connect to MS SQL Server -->
            <appender name="ADONetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">
                <bufferSize value="1" />
                <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <connectionString value="data source=SQLSVR;initial catalog=test_log4net;integrated security=false;persist security info=True;User ID=sa;Password=sa" />
                <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
                <parameter>
                    <parameterName value="@log_date" />
                    <dbType value="DateTime" />
                    <!-- 
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
                    </layout> 
                    -->
                    <layout type="log4net.Layout.RawTimeStampLayout" />
                </parameter>
                <parameter>
                    <parameterName value="@thread" />
                    <dbType value="String" />
                    <size value="255" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%thread" />
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@log_level" />
                    <dbType value="String" />
                    <size value="50" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%level" />
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@logger" />
                    <dbType value="String" />
                    <size value="255" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%logger" />
                    </layout>
                </parameter>
                <parameter>
                    <parameterName value="@message" />
                    <dbType value="String" />
                    <size value="4000" />
                    <layout type="log4net.Layout.PatternLayout">
                        <conversionPattern value="%message" />
                    </layout>
                </parameter>
            </appender>
            <!-- Setup the root category, add the appenders and set the default level -->
            <root>
                <level value="WARN" />
                <appender-ref ref="LogFileAppender" />
                <appender-ref ref="ConsoleAppender" />
                <!-- <appender-ref ref="ADONetAppender_SqlServer" /> -->
                <appender-ref ref="NetSendAppender" />
                <!-- <appender-ref ref="A" /> -->
            </root>
            <!-- Specify the level for some specific categories -->
            <logger name="ConsoleApp.LoggingExample">
                <!-- <appender-ref ref="B" /> -->
                <level value="ALL" />
                <appender-ref ref="RollingLogFileAppender" />
            </logger>
        </log4net>
    </configuration>

    另外一个配置注释:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,
                         Version=3.3.3.3001, Culture=neutral, PublicKeyToken=aa95f207798dfdb4" />
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
      </configSections>
      <log4net>
        <!--定义输出到文件中-->
        <appender name="LogFileAppender"  type="log4net.Appender.RollingFileAppender,log4net">
          <!--<appender name="LogFileAppender" type="log4net.Appender.FileAppender">-->
          <!--定义文件存放位置-->
          <file value="log4netfile.log" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd-HH:mm:ss" />
          <layout type="log4net.Layout.PatternLayout">
            <!--输出格式-->
            <!--样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info-->
            <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 所在类:%logger property:[%property{NDC}] - 日志描述:%message%newline" />
          </layout>
        </appender>
        <!--定义输出到控制台命令行中-->
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          </layout>
        </appender>
        <!--定义输出到windows事件中-->
        <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          </layout>
        </appender>
        <!--定义输出到数据库中,这里举例输出到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>
    
      </log4net>
      
    </configuration>
  • 相关阅读:
    javascript中this使用规律
    call和apply的作用和不同
    SVN的标准目录结构:trunk、branches、tags
    SVN 多人修改,如何管理 关于版本的问题
    公司考勤系统项目设计
    CDI Features
    Java Design Pattern
    公司考勤系统设计文件
    spring( history Design Philosophy )
    JSON/xml
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/3751275.html
Copyright © 2020-2023  润新知