简体   繁体   中英

log4net is not logging to log file or console (external log4net.config file)

I created aa file called log4net.config in my project and added the following configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="console" />
      <appender-ref ref="RollingFileAppender" />
    </root>
    <appender name="console" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="${LOCALAPPDATA}\MyApp\LogFile.log" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%-5level [%d{yyyy-MM-dd hh:mm:ss}] [%thread] (Line:%line) %M: - %m%n" />
      </layout>
    </appender>
  </log4net>
</configuration>

After that, I added the line below to my AssemblyInfo.cs file:

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

In the application, I have the following static class:

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


        public static void LogInfo(string msg)
        {
            log.Info(msg);
        }

        public static void LogDebug(string msg)
        {
            log.Debug(msg);
        }

        public static void LogWarn(string msg, Exception e)
        {
            log.Warn(msg, e);
        }

        public static void LogError(string msg, Exception e)
        {
            log.Error(msg, e);
        }
    }

Then I'm trying to log somewhere in the application in another class using Logger.LogDebug("Error logger working.");

However, I can't see any log file being created or written to it. What am I doing wrong?

UPDATE:

I also added the following in my app.config file but I see nothing at all on my Output console...

<appSettings>
    <add key="log4net.Internal.Debug" value="true"/>
</appSettings>

If you have the log4net configuration in a log4net logging configuration file, you do not have to add the configuration of the web.config (configuration tag and sections). Just add the log4net tags:

<log4net>
<root>
  <level value="ALL" />
  <appender-ref ref="console" />
  <appender-ref ref="RollingFileAppender" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger - %message%newline" />
  </layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="${LOCALAPPDATA}\MyApp\LogFile.log" />
  <param name="AppendToFile" value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%-5level [%d{yyyy-MM-dd hh:mm:ss}] [%thread] (Line:%line) %M: - %m%n" />
  </layout>
</appender>
</log4net>

Managed to find out the issue thanks to stuartd's comment. My problem was, my full solution had 2 projects. I had only configured one project (which is not the start-up project) for log4net.

My original log4net configuration did not have any mistake. I simply added log4net reference to my start up project and added log4net configuration to that project as well and it started logging.

Many thanks to everyone who tested my config.

I could not reproduce your mistake. Configure log4net in AssemblyInfo works fine. maybe the problem in configuration. let me show you another implementation

    private static readonly ILog log = LogManager.GetLogger("myLogger");
    private static string configFile = "log4net.config";
    static Logger()
    {
        XmlConfigurator.Configure(new FileInfo(configFile));
    }

log4net.config:

<log4net>
<logger name="myLogger">
  <level value="ALL" />
  <appender-ref ref="console" />
  <appender-ref ref="RollingFileAppender" />
</logger>
<appender name="console" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger - %message%newline" />
  </layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="${LOCALAPPDATA}\MyApp\LogFile.log" />
  <param name="AppendToFile" value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%-5level [%d{yyyy-MM-dd hh:mm:ss}] [%thread] (Line:%line) %M: - %m%n" />
  </layout>
</appender>
</log4net>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM