zoukankan      html  css  js  c++  java
  • log4net的简单使用记录一下,防止下次忘记

      1.在程序的assembly.cs下添加: [assembly: log4net.Config.XmlConfigurator(Watch = true)]  (忘记这一步,弄了半天,上次好像也是这样,不常用还是记录下好)

              对于log配置参数读取可制定config文件: [assembly: XmlConfigurator(ConfigFile="Log4net.config",Watch=true)]  //此处读取config就是读取log4net.config的文件;

            2.程序引用log4net,定义记录Log的实例 : ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().GetType());

            3.配置文件:        

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <!-- Register a section handler for the log4net section -->
    <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
    </configSections>
    <!-- This section contains the log4net configuration settings -->
    <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log\" />
    <!-- Example using environment variables in params -->
    <!-- <file value="${TMP}log-file.txt" /> -->
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd&quot;.txt&quot;" />
    <StaticLogFileName value="false"/>
    <!-- An alternate output encoding can be specified -->
    <!-- <encoding value="unicodeFFFE" /> -->
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger - %m%n" />
    </layout>
    <!-- Alternate layout using XML
    <layout type="log4net.Layout.XMLLayout" /> -->
    </appender>
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date-->%m%n" />
    </layout>
    </appender>
    <!-- Setup the root category, add the appenders and set the default level -->
    <root>

    <!--记录日志的级别,ALL是记录所有的,debug,info,warn,error-->

    <level value="ALL" />
    <appender-ref ref="LogFileAppender" />
    <!--appender-ref ref="TraceAppender" /-->
    </root>
    </log4net>

    </configuration>

  • 相关阅读:
    Android 如何在自己的应用中模拟Home键
    ComboBox添加Item
    修复JSCRIPT
    几天没用TFS!挂了!
    简单的上传图片并生成缩略图
    上传图片时中文路径的问题
    获取DataList和Repeater里的控件
    MIME 类型列表
    STL注意
    MFC下的MessageBox使用_附带CBUTTON
  • 原文地址:https://www.cnblogs.com/muzililong/p/10413138.html
Copyright © 2011-2022 走看看