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. Solr4.3之拼写检查Spellcheck功能

    原文地址:http://www.656463.com/article/iaquii.htm 拼写检查功能,能在搜索时提供一个较好用户体验,所以,主流的搜索引擎都有这个功能,在这之前,笔者先简单的说一下 ...

  2. 国外it网站收集

    1.http://news.com.com/2.http://www.zdnet.com/3.http://www.salon.com/tech/index.html4.http://www.brin ...

  3. 三种查看SqlServer中数据物理pge页的方法

    1.根据数据记录查看当前记录所在的文件编号.page页.以及在页中的插槽. 示例如下: SELECT top %%physloc%%, sys.fn_physlocFormatter (%%physl ...

  4. C++ 自动指针 共享指针

    #include <iostream> #include <string> #include <memory> class Item { public: Item( ...

  5. javascript值和引用

    JavaScript引用指向的是值. 简单值(即标量基本类型值,基本类型值,js中6类,null.undefined.boolean.number.string和symbol)总是通过值复制的方式来赋 ...

  6. dom classList

    才发现dom对象就有classList属性,通过它可以判断该dom是否有指定的class名存在. var tar = e.target; var classList = tar.classList; ...

  7. NSArray和NSDictionary添加空对象,以及nil和Nil和NULL和NSNull

    因为在NSArray和NSDictionary中nil中有特殊的含义(表示列表结束),所以不能在集合中放入nil值.如要确实需要存储一个表示“什么都没有”的值,可以使用NSNull类. NSNull只 ...

  8. Android笔记:Socket通讯常见问题

    经验证的socket通讯问题 1.如果是模拟器和本机PC直接通讯,需要使用本机IP地址 而不是 10.0.2.2  如本机的静态地址为192.168.1.2 则直接使用该地址 2.接收和连接代码不能在 ...

  9. golang json string remove field

    golang中如何移除多余的field? 同样是json结构,不能像js 的json一样 delete key 直接移除,网上找了很多相似的,还没找到解决办法,先mark一下 感谢大神提供解决思路,设 ...

  10. OrderSessionHelper查看订单在session是否存在的辅助类

    1. package com.biotool.web.controller.helper; import org.apache.commons.lang3.StringUtils; import ja ...