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. AMQP协议

    当前各种应用大量使用异步消息模型,并随之产生众多消息中间件产品及协议,标准的不一致使应用与中间件之间的耦合限制产品的选择,并增加维护成本. AMQP是一个提供统一消息服务的应用层标准协议,基于此协议的 ...

  2. Eclipse中直接双击执行bat时路径问题

    之前bat中使用的是 cd %cd% 这样在文件夹中直接运行bat是没问题的 但在eclipse中运行, 取得的路径就是eclipse.exe的所在路径 而如果需要获得bat文件的实际所在路径 应该使 ...

  3. Magento PDF发票,支持中文,以及修改的办法

    Magento PDF发票,支持中文,以及修改的办法.   如果让magento的PDF发票支持中文.Magento生成PDF发票.使用的是zend framework的zend_pdf类. 下面是一 ...

  4. Estimating Project Costs

    The Wideman Comparative Glossary of Common Project Management Terms describes estimating cost as, &q ...

  5. Ubuntu 14.04安装OpenCV 3.1

    从OpenCV官网上下载OpenCV官网上下载OpenCV的未编译源代码: 点击这里 国内很多网络打开OpenCV官网速度缓慢,可以点击如下地址直接从GitHub上下载OpenCV 3.1的源代码 下 ...

  6. 《JAVA NIO》Channel

    3.通道 Channle主要分为两类:File操作对应的FIleChannel和Stream操作对应的socket的3个channe. 1.这3个channel都是抽象类.其具体实现在SPI里面. 2 ...

  7. LeetCode Peeking Iterator

    原题链接在这里:https://leetcode.com/problems/peeking-iterator/ 题目: Given an Iterator class interface with m ...

  8. 使用pycharm远程调试python代码

    使用 pycharm 进行 python 代码远程调试 pycharm 的远程调试是从远程机器连接到本地机器,需要在远程机器的py文件中指定本地机器的IP和端口. 远程机器上,通过easy_insta ...

  9. 指向函数的指针与iOS-Block相关知识

    指向函数的指针与iOS-Block相关知识 一. 函数指针的定义和调用: 关于函数指针的知识详细可参考:http://www.cnblogs.com/mjios/archive/2013/03/19/ ...

  10. bootstrap实战经验

    凡是基本的布局需要float实现的,都可以考虑利用网格布局. 1,.jumbotron可以形成一个青灰色的背景,并自动调节对应边距 2,.panel的应用十分广泛,可以自动设置合适的padding.甚 ...