1.下载log4net (Google log4net) //已有

2.unzip log4net

3.运行VS,新建 c# Windows应用程序。

4.添加引用Log4NET

5.新建一个应用程序配置文件App.config(具体内容附在后面)

6.打开Form1.cs,

在Namespace上添加一行 [assembly: log4net.Config.DOMConfigurator(Watch=true)] 
(或者 编辑Assembly.cs文件,添加如下内容: 
[assembly:log4net.Config.DOMConfigurator( ConfigFileExtension="config",Watch=true)] )

也可以在properties下面的AssemblyInfo.cs下添加

[assembly:log4net.Config.XOMConfigurator( ConfigFileExtension="config",Watch=true)] )

在类Form1中添加一个静态变量

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

7.添加一个按钮。在按钮处理函数中添加一行 log.Warn("你好!");

8.运行程序。点一下按钮。

OK,打开Bin\Debug\log-file.txt,可以看到“你好”。

附.App.config

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

<appSettings>

<!-- To enable internal log4net logging specify the following appSettings key -->

<!-- <add key="log4net.Internal.Debug" value="true"/> -->

</appSettings>

<!-- This section contains the log4net configuration settings -->

<log4net>

<!-- Define some output appenders -->

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">

<param name="File" value="log-file.txt" />

<!-- Example using environment variables in params -->

<!-- <param name="File" value="${TMP}\\log-file.txt" /> -->

<param name="AppendToFile" value="true" />

<!-- An alternate output encoding can be specified -->

<!-- <param name="Encoding" value="unicodeFFFE" /> -->

<layout type="log4net.Layout.PatternLayout">

<param name="Header" value="[Header]\r\n" />

<param name="Footer" value="[Footer]\r\n" />

<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />

</layout>

<!-- Alternate layout using XML

<layout type="log4net.Layout.XMLLayout" /> -->

</appender>

<!-- Setup the root category, add the appenders and set the default level -->

<root>

<level value="ALL" />

<appender-ref ref="LogFileAppender" />

<!-- <appender-ref ref="A" /> -->

</root>

<!-- Specify the level for some specific categories -->

<logger name="SLog4net.Form1">

<!-- <appender-ref ref="B" /> -->

<level value="ALL" />

<appender-ref ref="RollingLogFileAppender" />

</logger>

</log4net>

</configuration>

--------------------------------App.config

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

<appSettings>

<!-- To enable internal log4net logging specify the following appSettings key -->

<!-- <add key="log4net.Internal.Debug" value="true"/> -->

</appSettings>

<!-- This section contains the log4net configuration settings -->

<log4net>

<!-- Define some output appenders -->

<appender name="LogFileAppender" type="log4net.Appender.FileAppender">

<param name="File" value="log-file.txt" />

<!-- Example using environment variables in params -->

<!-- <param name="File" value="${TMP}\\log-file.txt" /> -->

<param name="AppendToFile" value="true" />

<!-- An alternate output encoding can be specified -->

<!-- <param name="Encoding" value="unicodeFFFE" /> -->

<layout type="log4net.Layout.PatternLayout">

<param name="Header" value="[Header]\r\n" />

<param name="Footer" value="[Footer]\r\n" />

<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />

</layout>

<!-- Alternate layout using XML

<layout type="log4net.Layout.XMLLayout" /> -->

</appender>

<!-- Setup the root category, add the appenders and set the default level -->

<root>

<level value="ALL" />

<appender-ref ref="LogFileAppender" />

<!-- <appender-ref ref="A" /> -->

</root>

<!-- Specify the level for some specific categories -->

<logger name="SLog4net.Form1">

<!-- <appender-ref ref="B" /> -->

<level value="ALL" />

<appender-ref ref="RollingLogFileAppender" />

</logger>

</log4net>

</configuration>

//调用------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using log4net;

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

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

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

private void button1_Click(object sender, EventArgs e)

{

log.Warn("你好!");

}

}

log4net 使用教程的更多相关文章

  1. 在C#代码中应用Log4Net系列教程

    在C#代码中应用Log4Net系列教程(附源代码)   Log4Net应该可以说是DotNet中最流行的开源日志组件了.以前需要苦逼写的日志类,在Log4Net中简单地配置一下就搞定了.没用过Log4 ...

  2. 在C#代码中应用Log4Net系列教程(附源代码)地址

    在博客园看到一篇关于Log4Net使用教程,比较详细,感谢这位热心的博主 博客园地址:http://www.cnblogs.com/kissazi2/archive/2013/10/29/339359 ...

  3. 在C#代码中应用Log4Net系列教程(附源代码)

    Log4Net应该可以说是DotNet中最流行的开源日志组件了.以前需要苦逼写的日志类,在Log4Net中简单地配置一下就搞定了.没用过Log4Net,真心不知道原来日志组件也可以做得这么灵活,当然这 ...

  4. LOG4NET图文教程

    LOG4NET教程 一:简介 从操作系统到大多数的大型软件,都会有自己的程序运行时的日志跟踪API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而软件开发人员需要一套强大的日志系统来记 ...

  5. [转]在C#代码中应用Log4Net系列教程(附源代码)

    Log4Net应该可以说是DotNet中最流行的开源日志组件了.以前需要苦逼写的日志类,在Log4Net中简单地配置一下就搞定了.没用过Log4Net,真心不知道原来日志组件也可以做得这么灵活,当然这 ...

  6. Log4Net使用教程

    简介 为方便跟踪程序运行情况,我们可以记录系统运行异常日志,winform和web都可以通过继承异常或者try来实现. 官方网站:http://logging.apache.org/log4net/ ...

  7. log4net 入门教程

    1.下载dll 下载地址:http://mirror.reverse.net/pub/apache/logging/log4net/binaries/ github:https://github.co ...

  8. 动态修改log4net组件的日志文件名

    最近项目使用到log4net来记录日志,当然二话不说先到cnblogs上查看一下各位高手关于log4net的教程和心得主要参看了摩诘 的Log4Net使用指南 (确实是非常好的log4net的入门指南 ...

  9. Log4Net快速配置

    1. Log4NET的概念: a) 级别:trace.debug.info.warn.error.fatal.常用debug(调试信息,程序员临时跟踪执行,在正式运行的项目中应该不显示):warn(警 ...

随机推荐

  1. java中计算两个日期之间天数的程序设计。

    //用java编写出一个以下方法计算两个日期之间天数的程序设计. import java.util.regex.Matcher; import java.util.regex.Pattern; pub ...

  2. 高效的两段式循环缓冲区──BipBuffer

    Simon Cooke,美国 (原作者) 北京理工大学 20981 陈罡(翻译) 写在前面的话: 循环缓冲区是一个非常常用的数据存储结构,已经被广泛地用于连续.流数据的存储和通信应用中.对于循环缓冲区 ...

  3. recordcount

    rs.recordcount 有时不能取到数,这时 要更改游标为客户端游标 .

  4. SQL Server 2008 R2不支持limit(限制行数)

    SQL Server 2008 R2不支持limit 可用:select top 3 * from Websites2 MySQL 语法 SELECT *FROM PersonsLIMIT 5; Or ...

  5. thinkPHP 接支付宝及时到账接口

    支付宝及时到帐接口,现在整理以下: 1.先将支付宝提供的公共类库函数库文件防盗thinkPHP的Vender目录下建的一个alipay文件下,以便之后的调用. //四个文件我分别给他们改了下名字,因为 ...

  6. imx6 system boot

    imx6开机启动就进入download模式,有的板子进入文件系统之后会进入download模式.查看datasheet,Chapter 8 System Boot查找原因,记录于此. freescal ...

  7. gulp教程

    1. http://www.tuicool.com/articles/FJVNZf 2.http://www.ydcss.com/archives/18 3.手动创建package.json: 如:c ...

  8. saltstack之(六)配置管理state

    配置管理是saltstack工具最重要的模块之一,也是学习saltstack之后使用最多的一个功能.可以轻松实现上百台上千台甚至上万台服务器的管理工作. 1.使用state模块进行配置管理,编写sls ...

  9. centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课

    centos   shell编程6一些工作中实践脚本   nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志  直接送给bc做计算  gzip  innobacku ...

  10. iptables 基础知识

    [root@tp ~]#iptables -L -n 查看防火墙规则 [root@tp ~]# iptables -D INPUT 1  根据命令iptables -L -n --line-numbe ...