zoukankan      html  css  js  c++  java
  • 如何在WindowsForm及Web开发中用Log4net

    开发中使用Log4Net来生成Log文件,常因为缺少某个步骤,导致最后Log无法生成。

    前提:利用NuGet Package Manager,在其中检索,并安装Log4Net包,会自动生成需要的Dll文件及配置文件。

    如下,在根目录下的App.Config中增加如下代码:(一般会自动生成,根据需要修改)

     View Code

    1)在项目启动阶段,Program.cs中

    static class Program
        {
    
            [STAThread]
            static void Main()
            {
                log4net.Config.XmlConfigurator.Configure();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }
    View Code

    2)在共同的Log出力处,

    public readonly static log4net.ILog logger =
    log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    注意:最终生成Log文件夹的权限,及遗漏步骤1)中工程启动时,增加XML文件的读取。否则Log生成不出来,还不报错。

    Love it, and you live without it
  • 相关阅读:
    11.2~11.8 每周总结
    11.30~11.6 每周总结
    架构之美 图书笔记 03
    每周总结 10.26-11.1
    每周总结 10.19-10.25
    每周总结 10.12-10.18
    [Tips] centos下docker服务开机自启动
    [Notes] Linux内网穿透
    [Tips] python 文件读写
    [Tips] python 文件读写
  • 原文地址:https://www.cnblogs.com/tomclock/p/6902038.html
Copyright © 2011-2022 走看看