<?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);

附带详细注释的log4net的app.config文件配置例子的更多相关文章

  1. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  2. Winform 数据库连接app.config文件配置 数据库连接字符串

    1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...

  3. C# app.config文件配置和修改

    很多时候我们需要对系统的.config文件进度读写操作,例如:系统初始化的参数的更改.系统参数的改变都需要更新到配置文件. 首先我们有必要了解一下app.config.exe.config和vshos ...

  4. Winform数据库连接app.config文件配置

    1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...

  5. c#Winform程序调用app.config文件配置数据库连接字符串

    你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings name="  " connectionString= ...

  6. C#中App.config文件配置获取

    最新的framework使用如下方法: using System.Configuration; ConfigurationManager.AppSettings["key"]; A ...

  7. C# App.config文件配置数据的读写

    添加程序集引用  System.configuration.dll 和命名空间 using System.Configuration; 读: ConfigurationManager.AppSetti ...

  8. Visual Studio 2013 Unit Test Project App.config文件设置方法

    开放中经常会要做单元测试,新的项目又没有单元测试项目,怎么才能搭建一个单元测试项目呢? 下面跟我四步走,如有错误之处,还请指正! 1.添加项目 2.添加配置文件 新建app.config文件,注意不是 ...

  9. C#项目实例中读取并修改App.config文件

    C#项目是指一系列独特的.复杂的并相互关联的活动,这些活动有着一个明确的目标或目的,必须在特定的时间.预算.资源限定内,依据规范完成.项目参数包括项目范围.质量.成本.时间.资源. 1. 向C#项目实 ...

随机推荐

  1. Robot Framework自动化测试(二)---元素定位

    说明: 不要误认为Robot framework 只是个web UI测试工具,更正确的理解Robot framework是个测试框架,之所以可以拿来做web UI层的自动化是国为我们加入了seleni ...

  2. Xcode_cocoaPods-超详细傻瓜式安装教程

    一.Ruby环境: 下载cocoaPods需要Ruby环境. 1. Mac os 10.5以后只带Ruby环境.为了确保万无一失还是查看一下吧. 打开终端 (1)ruby -v (2)更新tuby g ...

  3. servlet、filter、listener、interceptor之间的区别和联系

    一.概念 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性,并且可以动态的生成web页面,它工作在客户端请求与服务器响应的中间层. 2.filter: ...

  4. eclipse svn插件安装方法

    eclipse svn插件安装方法 使用dropins安装插件 从Eclipse3.5开始,安装目录下就多了一个dropins目录.只要将插件解压后拖到该目录即可安装插件.比如安装svn插件subcl ...

  5. C#使用 UdpClient 类进行简单通信的例子

    UdpClient 类提供了一些简单的方法,用于在阻止同步模式下发送和接收无连接 UDP 数据报. 因为 UDP 是无连接传输协议,所以不需要在发送和接收数据前建立远程主机连接.但您可以选择使用下面两 ...

  6. 【循序渐进学Python】11.常用标准库

    安装完Python之后,我们也同时获得了强大的Python标准库,通过使用这些标准库可以为我们节省大量的时间.这里是一些常用标准库的简单说明.更多的标准库的说明,可以参考Python文档 sys 模块 ...

  7. WCF小结

    WCF总结 1.WCF(原代号为Indigo)是一个用于创建和运行分布式系统的技术集合,使用它能创建安全的.可靠的.跨平台的的分布式解决方案.它的面向服务的编程模型,整合了.Net平台下以往全部的分布 ...

  8. JPA学习(1)基础认知

    JPA 是什么 Java Persistence API:用于对象持久化的API. Java EE 5.0 平台标准的 ORM 规范,使得应用程序以统一的方式访问持久层: JPA和Hibernate的 ...

  9. HDU 5694---BD String

    HDU   5694 Problem Description 众所周知,度度熊喜欢的字符只有两个:B和D.今天,它发明了一种用B和D组成字符串的规则:S(1)=BS(2)=BBDS(3)=BBDBBD ...

  10. 启动Tomcat出现“Bad version number in .class file (unable to load class XXX)”解决

    转载自:http://blog.csdn.net/justdb/article/details/8067887 主要是jdk版本的不搭配 保证tomcat 的jdk版本 项目的jdk版本 还有就是编译 ...