Log4net does not write the log in the log file

后端 未结 13 2305
一生所求
一生所求 2020-11-29 17:09

I have created a simple scenario using Log4net, but it seems that my log appenders do not work because the messages are not added to the log file.

I added the followi

相关标签:
13条回答
  • 2020-11-29 17:37

    Insert:

     [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    

    at the end of AssemblyInfo.cs file

    0 讨论(0)
  • 2020-11-29 17:37

    In my case, log4net wasn't logging properly due to having a space in my project name. Drove me nuts why the same code worked just fine in a different project, but didn't in the new one. Spaces. A simple space.

    So, beware spaces in project names. I've learned my lesson.

    0 讨论(0)
  • 2020-11-29 17:41

    Your config file seems correct. Then, you have to register your Log4net config file to application. So you can use below code:

    var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
    XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
    

    After registering process, you can call below definition to call logger:

    private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
    log.Error("Sample log");
    
    0 讨论(0)
  • 2020-11-29 17:42

    Also, Make sure the "Copy always" option is selected for [log4net].config

    0 讨论(0)
  • 2020-11-29 17:44

    Use this FAQ page: Apache log4net Frequently Asked Questions

    About 3/4 of the way down it tells you how to enable log4net debugging by using application tracing. This will tell you where your issue is.

    The basics are:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="log4net.Internal.Debug" value="true"/>
        </appSettings>
    </configuration>
    

    And you see the trace in the standard output

    0 讨论(0)
  • 2020-11-29 17:50

    For me I moved the location of the logfiles and it was only when I changed the name of the file to something else it started again.

    It seems if there is a logfile with the same name already existing, nothing happens.

    Afterwards I rename the old file and changed the log filename in the config back again to what it was.

    0 讨论(0)
提交回复
热议问题