欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Log4Net 使用 FileAppender (log4net 1.2.10.0)

发布时间:2025/4/5 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Log4Net 使用 FileAppender (log4net 1.2.10.0) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
  • 项目中引入log4net.dll
  • Global.asax.cs 中加入
    [assembly: log4net.Config.XmlConfigurator(ConfigFileExtension="log4net",Watch=true)]
  • Global.asax.cs 的 Application_Start() 方法中加入
    log4net.Config.XmlConfigurator.Configure();
  • 项目中新建一个 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>
  • 建立 logfile.txt 文件,并给写权限
  • 使用
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    using log4net;
    using log4net.Config;

    namespace WebApp
    {
        
    /**//// <summary>
        
    /// Summary description for WebForm1.
        
    /// </summary>

        public class WebForm1 : System.Web.UI.Page
        
    {
            
    private static readonly ILog log = log4net.LogManager.GetLogger(typeof(WebForm1));

            
    protected System.Web.UI.WebControls.Button Button1;

            
    private void Page_Load(object sender, System.EventArgs e)
            
    {

            }


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

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

            private void InitializeComponent()
            
    {    
                
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
                
    this.Load += new System.EventHandler(this.Page_Load);

            }

            
    #endregion


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

        }

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

    总结

    以上是生活随笔为你收集整理的Log4Net 使用 FileAppender (log4net 1.2.10.0)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。