Log4Net 使用 FileAppender (log4net 1.2.10.0)

  1. 项目中引入log4net.dll
  2. Global.asax.cs 中加入
    [assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
  3. Global.asax.cs 的 Application_Start() 方法中加入
    log4net.Config.XmlConfigurator.Configure();
  4. 项目中新建一个 log4net 的配置文件 App.dll.log4net
    <? xml version="1.0" encoding="utf-8"  ?>
    < log4net >
        
    < appender  name ="FileAppender"  type ="log4net.Appender.FileAppender" >
            
    < file  value ="C:\Inetpub\wwwroot\WebApp\logfile.txt"   />
            
    < appendToFile  value ="true"   />
            
    < layout  type ="log4net.Layout.PatternLayout" >
                
    < conversionPattern  value ="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"   />
            
    </ layout >
        
    </ appender >
        
    < root >
            
    < level  value ="INFO"   />
            
    < appender-ref  ref ="FileAppender"   />
        
    </ root >
    </ log4net >

  5. 建立 logfile.txt 文件,并给写权限
  6. 使用
    None.gif using  System;
    None.gif
    using  System.Collections;
    None.gif
    using  System.ComponentModel;
    None.gif
    using  System.Data;
    None.gif
    using  System.Drawing;
    None.gif
    using  System.Web;
    None.gif
    using  System.Web.SessionState;
    None.gif
    using  System.Web.UI;
    None.gif
    using  System.Web.UI.WebControls;
    None.gif
    using  System.Web.UI.HtmlControls;
    None.gif
    None.gif
    using  log4net;
    None.gif
    using  log4net.Config;
    None.gif
    None.gif
    namespace  WebApp
    ExpandedBlockStart.gifContractedBlock.gif
    dot.gif {
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    /**//// <summary>
    InBlock.gif    
    /// Summary description for WebForm1.
    ExpandedSubBlockEnd.gif    
    /// </summary>

    InBlock.gif    public class WebForm1 : System.Web.UI.Page
    ExpandedSubBlockStart.gifContractedSubBlock.gif    
    dot.gif{
    InBlock.gif        
    private static readonly ILog log = log4net.LogManager.GetLogger(typeof(WebForm1));
    InBlock.gif
    InBlock.gif        
    protected System.Web.UI.WebControls.Button Button1;
    InBlock.gif
    InBlock.gif        
    private void Page_Load(object sender, System.EventArgs e)
    ExpandedSubBlockStart.gifContractedSubBlock.gif        
    dot.gif{
    InBlock.gif
    ExpandedSubBlockEnd.gif        }

    InBlock.gif
    ContractedSubBlock.gifExpandedSubBlockStart.gif        
    Web Form Designer generated code#region Web Form Designer generated code
    InBlock.gif        
    override protected void OnInit(EventArgs e)
    ExpandedSubBlockStart.gifContractedSubBlock.gif        
    dot.gif{
    InBlock.gif            
    //
    InBlock.gif            
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    InBlock.gif            
    //
    InBlock.gif
                InitializeComponent();
    InBlock.gif            
    base.OnInit(e);
    ExpandedSubBlockEnd.gif        }

    InBlock.gif        
    ExpandedSubBlockStart.gifContractedSubBlock.gif        
    /**//// <summary>
    InBlock.gif        
    /// Required method for Designer support - do not modify
    InBlock.gif        
    /// the contents of this method with the code editor.
    ExpandedSubBlockEnd.gif        
    /// </summary>

    InBlock.gif        private void InitializeComponent()
    ExpandedSubBlockStart.gifContractedSubBlock.gif        
    dot.gif{    
    InBlock.gif            
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    InBlock.gif            
    this.Load += new System.EventHandler(this.Page_Load);
    InBlock.gif
    ExpandedSubBlockEnd.gif        }

    ExpandedSubBlockEnd.gif        
    #endregion

    InBlock.gif
    InBlock.gif        
    private void Button1_Click(object sender, System.EventArgs e)
    ExpandedSubBlockStart.gifContractedSubBlock.gif        
    dot.gif{
    InBlock.gif            log.Info(
    "这是我在使用Log4Net");
    ExpandedSubBlockEnd.gif        }

    ExpandedSubBlockEnd.gif    }

    ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/netflu/archive/2006/07/27/461226.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值