乱七八糟- log4net

使用log4net的日志记录
本文介绍如何在.NET应用程序中使用log4net进行日志记录。通过四个简单步骤,包括配置文件设置、应用程序初始化及日志记录示例,展示了log4net的基本用法。

This is simple usage of log4net

 

 By name we can know log4net is a logging tool for .net application, from log4j, it’s generic version on java platform is well known by developers.

 

       There are some simple steps to active a log by log4net in our .net code, what I record here is just a most simple case, and more parameters and layout define for log can be easily found from apache web.

 

       Setp1 of course you need add log4net dll to your application.

       Step2 create a config file and name is as “log4net.config”, like follow:

<?xml version="1.0"?>

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

  <configSections>

<section name="log4net"

type="log4net.Config.Log4NetConfigurationSectionHandler, log4net-net-1.2"/>

  </configSections>

  <log4net>

    <logger name="EPS.Logging">

      <level value="ALL"/>

      <appender-ref ref="LogFileAppender" />

    </logger>

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

      <param name="File" value=".//temp//" />

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

      <param name="MaxSizeRollBackups" value="100" />

      <param name="StaticLogFileName" value="false" />

      <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />

      <param name="RollingStyle" value="Date" />

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

        <param name="Header" value="[Header]&#13;&#10;"/>

        <param name="Footer" value="[Footer]&#13;&#10;"/>

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

      </layout>

    </appender>

  </log4net>

</configuration>

 

      Setp3 need let your application notice this config by add one line in AssemblyInfo.cs if you are running winform app or console app.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

If it’s a web or web service project you need add one more line at Global.ascx.cs like follow:

protected void Application_Start(object sender, EventArgs e)

 {log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));}

To let the config file be loaded when application is started.

 

Step4 as we config the log name as “EPS.Logging”, we can use it in our code like follow:

class Program

{

     static void Main(string[] args)

    {

            log4net.LogManager.GetLogger("EPS.Logging").Debug("begin log");

            log4net.LogManager.GetLogger("EPS.Logging").Fatal("end");

     }

}

 

The result is we can find the log file in temp folder like follow:

 

[Header]

2010-01-14 14:36:44,896 [10] DEBUG EPS.Logging [(null)]  - begin log

2010-01-14 14:36:44,911 [10] FATAL EPS.Logging [(null)]  - end

[Footer]

 

That’s all

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值