Log4Net config in external file does not work

前端 未结 13 2138
清歌不尽
清歌不尽 2020-11-30 20:27

We are using log4net and want to specify it\'s configuration in an external config file (as we have done with other sections). To do this we have changed the log4net section

13条回答
  •  余生分开走
    2020-11-30 21:07

    Assuming you had an external config file called log4net.config that is copied to your deploy directory you can configure it like so:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Reflection;
    
    using log4net;
    
    namespace MyAppNamespace {
        static class Program {
            //declare it as static and public so all classes in the project can access it
            //like so: Program.log.Error("got an error");
            public static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
    
            /// 
            /// The main entry point for the application.
            /// 
            [STAThread]
            static void Main() {
                 //configure it to use external file
                log4net.Config.XmlConfigurator.Configure(new Uri(Application.StartupPath + "\\log4net.config"));
                //use it
                log.Debug("#############   STARING APPLICATION    #################");
    
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FormMain());
            }
        }
    }
    

提交回复
热议问题