zoukankan      html  css  js  c++  java
  • Log4Net简单使用

         1.配置文件添加

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

    <log4net>
    <!--<logger name="logerror">
    <level value="ERROR" />
    <appender-ref ref="ErrorAppender" />
    </logger>-->
    <logger name="loginfo">
    <level value="INFO" />
    <appender-ref ref="InfoAppender" />
    </logger>
    <!--<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="Log\LogError\" />
    <param name="AppendToFile" value="true" />
    <param name="MaxSizeRollBackups" value="100" />
    <param name="MaxFileSize" value="10240" />
    <param name="StaticLogFileName" value="false" />
    <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
    <param name="RollingStyle" value="Date" />
    <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%n异常时间:%d %n异常级别:%-5p%n异常内容:%m%n" />
    </layout>
    --><!--&lt; &gt; = <> %n = 回车--><!--
    </appender>-->
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="LogInfo\" />
    <param name="AppendToFile" value="true" />
    <param name="MaxFileSize" value="10240" />
    <param name="MaxSizeRollBackups" value="100" />
    <param name="StaticLogFileName" value="false" />
    <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
    <param name="RollingStyle" value="Date" />
    <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="日志时间:%d %n日志级别:%-5p %n日志内容:%m%n%n" />
    </layout>
    </appender>
    </log4net>

    2.添加Log4Net引用文件

    3.配置[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]在 AssemblyInfo 下

    4.调用方法写日志

     1 public class LogHelper
     2 {
     3 public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
     4 
     5 public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
     6 
     7 //public static void SetConfig()
     8 //{
     9 // log4net.Config.XmlConfigurator.Configure();
    10 //}
    11 
    12 //public static void SetConfig(FileInfo configFile)
    13 //{
    14 // log4net.Config.XmlConfigurator.Configure(configFile);
    15 //}
    16 
    17 public static void WriteLog(string info)
    18 {
    19 if (loginfo.IsInfoEnabled)
    20 {
    21 loginfo.Info(info);
    22 }
    23 }
    24 
    25 public static void WriteErrorLog(string info)
    26 {
    27 if (logerror.IsErrorEnabled)
    28 {
    29 logerror.Error(info);
    30 }
    31 }
    32 }
  • 相关阅读:
    2-6 R语言基础 缺失值
    2-5 R语言基础 factor
    2-4 R语言基础 列表
    2-3 R语言基础 矩阵和数组
    2-2 R语言基础 向量
    【转】Python操作MongoDB数据库
    Python程序的执行原理
    数据分析的职业规划
    自定义菜单 开发 服务器繁忙
    微信自定义菜单
  • 原文地址:https://www.cnblogs.com/zhouyu2017/p/9580043.html
Copyright © 2011-2022 走看看