Log4Net config in external file does not work

前端 未结 13 2016
清歌不尽
清歌不尽 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 20:59

    Do you have the following attribute in your AssemblyInfo.cs file:

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

    and code like this at the start of each class that requires logging functionality:

    private static readonly ILog log = 
    LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    

    I have a blog post containing this and other info here.

    0 讨论(0)
  • 2020-11-30 20:59

    It is also possible to turn on a Debug mode for log4net. Insert this into the App.config file:

     <appSettings>
          <add key="log4net.Internal.Debug" value="true"/>
     </appSettings>
     <system.diagnostics>
          <trace autoflush="true">
          <listeners>
               <add
                 name="textWriterTraceListener"
                 type="System.Diagnostics.TextWriterTraceListener"
                 initializeData="link\to\your\file.log" />
          </listeners>
          </trace>
     </system.diagnostics>
    

    and you will at least get error messages, from which you can derive what exactly went wrong. In my case I simply forgot to set Copy to output directory to Copy Always.

    0 讨论(0)
  • 2020-11-30 21:05

    I had the same problem, besides having

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net")]
    

    in the running project, I also had the following line in a reference project:

    [assembly: log4net.Config.XmlConfigurator]
    

    When I removed this line in the referenced projects, the logs start to appear.

    0 讨论(0)
  • 2020-11-30 21:06

    Make sure that your log4net.config file is set with the following properties:

    Build Action: Content

    Copy to output directory: Copy Always

    0 讨论(0)
  • 2020-11-30 21:06

    In my case I got this error message:

    log4net: config file [C:\........\bin\Debug\log4net.config] not found.
    

    And log4net.config the file was in Visual Studio 2017 project, but when I checked the file in bin\Debug folder I could not find it.

    My original assembly setup was like this:

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

    After some research time, I change it to the following and it works:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = @"..\..\log4net.config", Watch = true)]
    
    0 讨论(0)
  • 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));
    
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [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());
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题