<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>

    <!--
    In a config file where there will (potentially) be more information stored beyond just the log4net configuration information, you will need to specify a
    section to identify where the log4net configuration is housed. Here is a sample section that specifies that the configuration information will be stored
    under the XML tag "log4net"
    -->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <log4net>
    <!--
    You need to have one root section to house your top-level logger references. These are the loggers that inherit information from your base logger (root).
    The only other thing that the root section houses is the minimum level to log. Since everything inherits from the root, no appenders will log information below
    that specified here. This is an easy way to quickly control the logging level in your application. Here is an example with a default level of
    INFO (which means DEBUG messages will be ignored) and a reference to two appenders that should be enabled under root:
    -->
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
    </root>

    <!--
    Sometimes you will want to know more about a particular part of your application. log4net anticipated this by allowing you to specify additional logger
    references beyond just the root logger.
    Note that the logger name is the full name of the class including the namespace. If you wanted to monitor an entire namespace, it would be as simple as listing
    just the namespace you wanted to monitor. I would recommend against trying to re-use appenders in multiple loggers. It can be done, but you can get some
    unpredictable results.
    Set logger name to * to target entire application
    -->
    <logger name="*">
      <!--<level value="DEBUG"/>-->
      <level value="ALL"/>
      <appender-ref ref="RollingFileAppender"/>
    </logger>

    <!--
    An appender is the name for what logs the information. It specifies where the information will be logged, how it will be logged, and under what circumstances
    the information will be logged. While each appender has different parameters based upon where the data will be going, there are some common elements.
    The first is the name and type of the appender. Each appender must be named (anything you want) and have a type assigned to it (specific to the type of
    appender desired)
    -->
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--
      Inside of each appender must be a layout section. This may be a bit different depending on the type of appender being used, but the basics are the same.
      You need a type that specifies how the data will be written. There are multiple options, but the one that I suggest you use is the pattern layout type.
      This will allow you to specify how you want your data written to the data repository. If you specify the pattern layout type, you will need a sub-tag that
      specifies a conversion pattern. This is the pattern by which your data should be written to the data repository.
      -->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline"/>
      </layout>

      <!-- Rolling File Appender specific settings. E.g. The name of the text file has to be specified -->
      <file value="c:\ApplicationLog\" />
      <appendToFile value="true" />
      <rollingStyle value="Date"/>
      <datePattern value="yyyy-MM-dd'.log'" />
      <maxSizeRollBackups value="90" />
      <staticLogFileName value="false" />

    </appender>

  </log4net>

  <system.diagnostics>
    <trace autoflush="true">
      <!-- Output the log4net internal debug messages by adding a trace listener -->
      <listeners>
        <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\ApplicationLog\log4net.log" />
      </listeners>
    </trace>
  </system.diagnostics>

</configuration>

配置文件设置完成后,需要引入下面三行代码

1. 在被跟踪的类外面加上:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

2. 在被跟踪的类中加上以下代码,创建私有的ILog实例

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

3. 在需要写log的地方,添加追加log的具体实现代码

log.Info("Initlaized");

log.Error("Error occured while initializing", ex);

最新文章

  1. 浅谈C语言中结构体的初始化
  2. [原] Android 自定义View步骤
  3. 准备NOIP2017 最长公共子序列(模版)
  4. C# web api返回类型设置为json的两种方法
  5. HTML5 canvas 绘图步骤
  6. taobao
  7. Jedis的JedisSentinelPool源代码分析
  8. 【转载】高性能I/O设计模式Reactor和Proactor
  9. structs 源码示例
  10. Ubuntu 10.04下安装Opengl glx
  11. Ext JS 6开发实例(四) :调整主视图
  12. Vue2全家桶之一:vue-cli(vue脚手架)超详细教程
  13. 教你用Windows自带工具给优盘/移动硬盘添加密码
  14. element-ui radio 再次点击取消选中
  15. jQuery位置操作
  16. 设置nginx中文件上传的大小限制度
  17. 简单的RNN和BP多层网络之间的区别
  18. CodeForces - 140E:New Year Garland (组合数&amp;&amp;DP)
  19. 2018-11-26 BIG DATA ANALYSIS
  20. .net Asp AdRotator(广告控件)

热门文章

  1. Azure开发者任务之一:解决Azure Storage Emulator初始化失败
  2. Sql server脏读、更新丢失、不可重复读、幻象读问题及解决方案
  3. 【循序渐进学Python】4. Python中的序列——字典
  4. Debian GNU/kFreeBSD是什么
  5. 泛函编程(9)-异常处理-Option
  6. 使用R的networkD3包画可交互的网络图
  7. 【iOS】Quartz2D简单介绍
  8. Android5.0新特性——兼容性(support)
  9. DOM应用实例(寻找房祖名)
  10. sql和access中截取字符串的区别