• C# log4net 的配置


    1、新建Web或者Webform项目,使用Nuget下载log4net

    2. 新建Config文件夹,将log4net配置文件放入此文件夹中(当修改web.config的时候会重启网站,所以把log4net单独出来比较好,也好管理), log4net的配置文件log4net_config.xml 

    在app.config或者web.config中的configuration节点下加入:放在configSections中第一位

    <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
    </configSections>

    CS:

    在Properties下的AssemblyInfo.cs文件中加入

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch = true)]

    如果我们将log4net帮助类放在另一个公共的类库项目中,又需要在另一个引用的项目中使用log4helper类,

    这时候仅需要在该项目里的Properties下的AssemblyInfo.cs文件中加入这句:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch = true)]

    日志配置:

    第一种:

    <?xml version="1.0" encoding="utf-8"?>
    
    <configuration>
    
      <system.web>
        <compilation debug="false" targetFramework="4.6.1" />
        <httpRuntime targetFramework="4.6.1" />
      </system.web>
    
      <log4net>
        <appender name="fatalAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="FATAL" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logsfatal.log" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="ERROR" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logserr.log" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="warnAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="WARN" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logswarn.log" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="INFO" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logsinfo.log" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="DEBUG" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logsdebug.log" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
          </layout>
        </appender>
        <appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
          <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="INFO" />
          </filter>
          <filter type="log4net.Filter.DenyAllFilter" />
          <File value="Logsperf.log" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %logger - %message%newline" />
          </layout>
        </appender>
        <root>
          <level value="ALL" />
          <appender-ref ref="fatalAppender" />
          <appender-ref ref="errorAppender" />
          <appender-ref ref="warnAppender" />
          <appender-ref ref="infoAppender" />
          <appender-ref ref="debugAppender" />
        </root>
      </log4net>
      
    </configuration>
    View Code

    第二种: 可以放在configuration种

    <log4net>
          <appender name="fatalAppender" type="log4net.Appender.RollingFileAppender">
            <filter type="log4net.Filter.LevelMatchFilter">
              <levelToMatch value="FATAL" />
            </filter>
            <filter type="log4net.Filter.DenyAllFilter" />
            <File value="LogsFatalfatal.log" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <datePattern value="yyyyMMdd" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
            </layout>
          </appender>
          <!-- 错误日志附加介质-->
          <appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
            <filter type="log4net.Filter.LevelMatchFilter">
              <levelToMatch value="ERROR" />
            </filter>
            <filter type="log4net.Filter.DenyAllFilter" />
    
            <param name="File" value="Log\" />
            <param name="AppendToFile" value="true" />
            <param name="MaxSizeRollBackups" value="100" />
            <param name="MaxFileSize" value="10240" />
            <param name="StaticLogFileName" value="false" />
            <param name="DatePattern" value="Error/yyyy-MM-dd/&quot;Error_&quot;yyyyMMdd&quot;.log&quot;"  />
            <param name="RollingStyle" value="Date" />
    
            <!-- 信息日志布局-->
            <layout type="log4net.Layout.PatternLayout">
              <!--<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />-->
              <param name="ConversionPattern" value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 记录类:%logger     操作者ID:%property{Operator} 操作类型:%property{Action}%n             当前机器名:%property%n当前机器名及登录用户:%username %n               记录位置:%location%n 消息描述:%property{Message}%n                    异常:%exception%n 消息:%message%newline%n%n" />
            </layout>
          </appender>
          <appender name="warnAppender" type="log4net.Appender.RollingFileAppender">
            <filter type="log4net.Filter.LevelMatchFilter">
              <levelToMatch value="WARN" />
            </filter>
            <filter type="log4net.Filter.DenyAllFilter" />
            <File value="LogsWarnwarn.log" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <datePattern value="yyyyMMdd" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
            </layout>
          </appender>
          <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
            <filter type="log4net.Filter.LevelMatchFilter">
              <levelToMatch value="INFO" />
            </filter>
            <filter type="log4net.Filter.DenyAllFilter" />
            <File value="LogsInfoinfo.log" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <datePattern value="yyyyMMdd" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
            </layout>
          </appender>
          <appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
            <filter type="log4net.Filter.LevelMatchFilter">
              <levelToMatch value="DEBUG" />
            </filter>
            <filter type="log4net.Filter.DenyAllFilter" />
            <File value="LogsDebugdebug.log" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <datePattern value="yyyyMMdd" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
            </layout>
          </appender>
          <appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
            <filter type="log4net.Filter.LevelMatchFilter">
              <levelToMatch value="INFO" />
            </filter>
            <filter type="log4net.Filter.DenyAllFilter" />
            <File value="Logsperf.log" />
            <appendToFile value="true" />
            <rollingStyle value="Date" />
            <datePattern value="yyyyMMdd" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%date %logger - %message%newline" />
            </layout>
          </appender>
          <root>
            <level value="ALL" />
            <appender-ref ref="fatalAppender" />
            <appender-ref ref="errorAppender" />
            <appender-ref ref="warnAppender" />
            <appender-ref ref="infoAppender" />
            <appender-ref ref="debugAppender" />
          </root>
        </log4net>
    View Code

    第三个示例

      <log4net>
        <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
        <!-- Set root logger level to ERROR and its appenders -->
        <root>
          <level value="ALL" />
          <appender-ref ref="SysAppender" />
        </root>
        <!-- Print only messages of level DEBUG or above in the packages -->
        <logger name="WebLogger">
          <level value="DEBUG" />
        </logger>
        <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
          <param name="File" value="App_Data/" />
          <param name="AppendToFile" value="true" />
          <param name="RollingStyle" value="Date" />
          <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
          <param name="StaticLogFileName" value="false" />
          <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>
        <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
          <layout type="log4net.Layout.PatternLayout,log4net">
            <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
          </layout>
        </appender>
      </log4net>
    View Code

    3. 在项目中

    log4net.ILog log = LogManager.GetLogger("log---"); //"log---" 会在日志文件中显示
            log4net.Config.XmlConfigurator.Configure(); //--这个执行一次就可以 log4net.config
            //配置log4--单独放一个文件中的
            //log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/log4net.config")));
            log.Info(DateTime.Now + "  这是一个log4net提示日志");
            log.Error(DateTime.Now + "  log4net错误日志");
    View Code

    4. 日志记录到数据库

    <?xml version="1.0" encoding="utf-8" ?>  
      
    <log4net debug="false">  
      <!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->  
      <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">  
        <!--日志缓存写入条数 设置为0时只要有一条就立刻写到数据库-->  
        <bufferSize value="0" />  
        <!--日志数据库连接串-->  
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />  
        <connectionString value="DATABASE=GoodLook;SERVER=xx.xx.140.170;UID=sa;PWD=123456a;Connect Timeout=30;" />  
        <!--日志数据库脚本-->  
        <commandText value="INSERT INTO LogDetails ([LogDate],[LogThread],[LogLevel],[LogLogger],[LogActionClick],[LogMessage],[UserName],[UserIP]) VALUES (@log_date, @thread, @log_level, @logger, @ActionsClick, @message,@UserName,@UserIP)" />  
        <!--日志时间LogDate -->  
         <parameter>  
          <parameterName value="@log_date" />  
          <dbType value="DateTime" />  
          <layout type="log4net.Layout.RawTimeStampLayout" />  
        </parameter>  
        <!--线程号-->  
        <parameter>  
          <parameterName value="@thread" />  
          <dbType value="String" />  
          <size value="100" />  
          <layout type="log4net.Layout.PatternLayout">  
            <conversionPattern value="%t" />  
          </layout>  
        </parameter>  
        <!--日志类型LogLevel -->  
        <parameter>  
          <parameterName value="@log_level" />  
          <dbType value="String" />  
          <size value="200" />  
          <layout type="log4net.Layout.PatternLayout">  
            <conversionPattern value="%p" />  
          </layout>  
        </parameter>  
        <!--日志名称-->  
        <parameter>  
          <parameterName value="@logger" />  
          <dbType value="String" />  
          <size value="500" />  
          <layout type="log4net.Layout.PatternLayout">  
            <conversionPattern value="%logger" />  
          </layout>  
        </parameter>  
        <parameter>  
          <parameterName value="@message" />  
          <dbType value="String" />  
          <size value="3000" />  
          <layout type="Log4NetApply.MyLayout">  
            <conversionPattern value="%property{Message}" />  
          </layout>  
        </parameter>  
        <parameter>  
          <parameterName value="@ActionsClick" />  
          <dbType value="String" />  
          <size value="4000" />  
            <layout type="Log4NetApply.MyLayout" >  
            <conversionPattern value = "%property{ActionsClick}"/>  
          </layout>  
        </parameter>  
        <!--自定义UserName -->  
        <parameter>    
            <parameterName value="@UserName" />    
            <dbType value="String" />    
            <size value="30" />  
          <layout type="Log4NetApply.MyLayout" >  
            <!--log4net.MDC.Set("UserName", "asdfasdf");  
            <conversionPattern value = "%X{UserName}"/>-->  
            <conversionPattern value = "%property{UserName}"/>  
          </layout>  
          </parameter>    
       <parameter>    
            <parameterName value="@UserIP" />    
            <dbType value="String" />    
            <size value="20" />  
            <layout type="Log4NetApply.MyLayout" >  
              <conversionPattern value = "%property{UserIP}"/>  
            </layout>  
          </parameter>    
      </appender>  
      <!-- setup the root category, add the appenders and set the default level -->  
      <root>  
        <level value="Warn"/><!-- 定义记录的日志级别-->  
        <level value="Info"/>  
        <level value="Debug"/>  
        <level value="Fine"/>  
        <appender-ref ref="ADONetAppender" /><!-- 记录到什么介质中-->  
      </root>  
      <!-- specify the level for some specific categories -->  
      <!--//关于上边root到logger这块,如果同时出现,有可能会出现重复插入记录的情况:-->  
      <!--<logger name="iNotes">  
        <level value="WARN"/>  
        <level value="INFO"/>  
        <level value="DEBUG"/>  
        <level value="FINE"/>  
        <appender-ref ref="ADONetAppender"/>  
      </logger>  
      <logger name="StellaLogger">  
        <level value="ALL"/>  
        <appender-ref ref="AdoNetAppender" />  
      </logger>-->  
      <appender name="ReflectionLayout" type="log4net.Appender.RollingFileAppender,log4net">  
      
        <!--日志文件路径,按文件大小方式输出时在这里指定文件名,并且前面的日志按天在文件名后自动添加当天日期形成文件-->  
        <param name="File" value="D:/Log/" />  
      
        <!--是否追加到文件-->  
        <param name="AppendToFile" value="true" />  
      
        <!--记录日志写入文件时,不锁定文本文件-->  
        <!--<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />-->  
      
        <!--Unicode编码-->  
        <!--<Encoding value="UTF-8" />-->  
      
        <!--最多产生的日志文件数,value="-1"为不限文件数-->  
        <!--<param name="MaxSizeRollBackups" value="10" />-->  
      
        <!--是否只写到一个文件中-->  
        <param name="StaticLogFileName" value="false" />  
      
        <!--按照何种方式产生多个日志文件(日期[Date],文件大小[Size],混合[Composite])-->  
        <param name="RollingStyle" value="Composite" />  
      
        <!--按日期产生文件夹,文件名[在日期方式与混合方式下使用]-->  
        <param name="DatePattern" value="yyyy-MM-dd/"ReflectionLayout.log""  />  
        <!--<param name="DatePattern" value="yyyyMMdd/yyyyMMdd"-TimerServer.log""  />-->  
        <!--<param name="DatePattern" value="yyyyMMdd/"TimerServer/TimerServer.log""  />-->  
      
        <!--每个文件的大小。只在混合方式与文件大小方式下使用,超出大小的在文件名后自动增加1重新命名-->  
        <param name="maximumFileSize" value="500KB" />  
      
        <!--记录的格式。-->  
        <layout type="TGLog.ExpandLayout2.ReflectionLayout,TGLog">  
          <param name="ConversionPattern" value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 记录类:%logger 操作者ID:%property{Operator} 操作类型:%property{Action}%n 当前机器名:%property%n当前机器名及登录用户:%username %n 记录位置:%location%n 消息描述:%property{Message}%n 异常:%exception%n 消息:%message%newline%n%n" />  
        </layout>  
      </appender>  
      
        
       
        
    </log4net> 
    View Code
    <bufferSize value="10" />,表示是记录10 条到缓冲区,满10条后再写入SQL server
    
    调试时可以改为1,实际应用建议为100
    
             <root>  
                <level value="WARN"/>  
                <level value="INFO"/>  
                <level value="DEBUG"/>  
                <level value="FINE"/>  
                <appender-ref ref="ADONetAppender" />  
            </root>      
    在需要记录的每个类中添加如下代码:
    private static log4net.ILog myLogger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);  
    View Code

    https://blog.csdn.net/kongwei521/article/details/52242319

    https://www.cnblogs.com/daretodream/p/3509027.html

    另外:可以找log4net帮助类

    链接:https://www.cnblogs.com/weixiaowei/p/8253228.html

  • 相关阅读:
    形象理解ERP(转)
    禁用windows server 2008 域密码复杂性要求策略
    How to adding find,filter,remove filter on display method Form
    Windows Server 2008 R2激活工具
    How to using bat command running VS development SSRS report
    Creating Your First Mac AppGetting Started
    Creating Your First Mac AppAdding a Track Object 添加一个 Track 对象
    Creating Your First Mac AppImplementing Action Methods 实现动作方法
    Creating Your First Mac AppReviewing the Code 审查代码
    Creating Your First Mac AppConfiguring the window 设置窗口
  • 原文地址:https://www.cnblogs.com/love201314/p/9089819.html
Copyright © 2020-2023  润新知