简体   繁体   English

log4net未记录到日志文件或控制台(外部log4net.config文件)

[英]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: 我在项目中创建了一个名为log4net.config文件,并添加了以下配置:

<?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: 之后,将以下行添加到我的AssemblyInfo.cs文件中:

[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."); 然后,我尝试使用Logger.LogDebug("Error logger working.");在另一个类的应用程序中某处登录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... 我还在app.config文件中添加了以下内容,但在输出控制台上什么也看不到...

<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). 如果在log4net日志记录配置文件中具有log4net配置,则不必添加web.config的配置(配置标记和部分)。 Just add the log4net tags: 只需添加log4net标签:

<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. 感谢stuartd的评论,设法找到了问题。 My problem was, my full solution had 2 projects. 我的问题是,我的完整解决方案有2个项目。 I had only configured one project (which is not the start-up project) for log4net. 我只为log4net配置了一个项目(不是启动项目)。

My original log4net configuration did not have any mistake. 我原来的log4net配置没有任何错误。 I simply added log4net reference to my start up project and added log4net configuration to that project as well and it started logging. 我只是将log4net参考添加到我的启动项目中,并将log4net配置也添加到该项目中,并且它开始记录日志。

Many thanks to everyone who tested my config. 非常感谢测试我的配置的每个人。

I could not reproduce your mistake. 我无法重现您的错误。 Configure log4net in AssemblyInfo works fine. 在AssemblyInfo中配置log4net可以正常工作。 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.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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