简体   繁体   English

Log4Net 创建空日志文件,而不是实际记录

[英]Log4Net creating empty log files, not actually logging

I've got an issue with my Log4Net usage in my application.我在应用程序中使用 Log4Net 时遇到问题。 This is my app.config file...这是我的 app.config 文件...

    <configuration>
  <log4net>
    <root>
      <level value="INFO"/>
      <appender-ref ref="FileAppender" />
    </root>
      <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="mylogfile.txt" />
        <appendToFile value="true" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
          <levelMin value="INFO" />
          <levelMax value="FATAL" />
        </filter>
      </appender>

  </log4net>


  <configSections>
    <section name="log4net"
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" 
         />
  rest of app.config under here...

This is at the top of my Program.cs这是我的 Program.cs 的顶部

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

I also have this at the top of my form partial class (is this a duplicate of the above?)我的表格部分 class 的顶部也有这个(这是上面的副本吗?)

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

And this code is on the form load (which does nothing)这段代码在表单加载中(什么都不做)

log.Info("Initializing...");
            log.Fatal("blah");

The logger creates a log file called "mylogfile.txt" but doesn't actually write anything into it.记录器创建一个名为“mylogfile.txt”的日志文件,但实际上并没有向其中写入任何内容。

I know I'm doing something wrong, but I can't spot what it is easily:( any help would be awesome.我知道我做错了什么,但我无法轻易发现它是什么:(任何帮助都会很棒。

I wound up figuring it out myself seconds later.几秒钟后我自己弄明白了。 The app.config was wrong (unsurprisingly, considering I wrote it). app.config 是错误的(不出所料,考虑到我写的)。

Here is the working app.config startup.这是正在运行的 app.config 启动。

<configuration>
  <configSections>
    <section name="log4net"
             type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"

             />
   <--- other section groups here, you will have these too --->
    </sectionGroup>
  </configSections>
  <log4net>
    <root>
      <level value="INFO"/>
      <appender-ref ref="FileAppender" />
    </root>
      <appender name="FileAppender" type="log4net.Appender.FileAppender">
        <file value="mylogfile.txt" />
        <appendToFile value="true" />
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">
          <levelMin value="INFO" />
          <levelMax value="FATAL" />
        </filter>
      </appender>

  </log4net>

I canned the我罐头

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

and replaced it with log4net.Config.XmlConfigurator.Configure();并将其替换为 log4net.Config.XmlConfigurator.Configure(); (here it is, where it is called in the Main program startup in Program.cs). (在这里,它在 Program.cs 的主程序启动中被调用)。

    static void Main()
    {
        log4net.Config.XmlConfigurator.Configure();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmMOEBackup());
    }

And it works!它有效! Thanks to everyone who viewed my question :)感谢所有看过我问题的人:)

http://log4net.sourceforge.net/release/1.2.0.30316/doc/manual/faq.html http://log4net.sourceforge.net/release/1.2.0.30316/doc/manual/faq.html

From the documentation:从文档:

How do I enable log4net internal debugging?如何启用 log4net 内部调试?

• To enable log4net's internal debug programmatically you need to set the log4net.helpers.LogLog.InternalDebugging property to true. • 要以编程方式启用log4net 的内部调试,您需要将log4net.helpers.LogLog.InternalDebugging 属性设置为true。 Obviously the sooner this is set the more debug will be produced.显然,设置得越早,产生的调试就越多。

• Internal debugging can also be enabled by setting a value in the application's configuration file (not the log4net configuration file, unless they log4net config is in the application's config file). • 也可以通过在应用程序的配置文件(不是log4net 配置文件,除非它们的log4net 配置在应用程序的配置文件中)中设置一个值来启用内部调试。 The log4net.Internal.Debug application setting must be set to the value true. log4net.Internal.Debug 应用程序设置必须设置为值 true。 For example:例如:

This setting is read immediatly on startup an will cause all internal debugging messages to be emmitted.此设置在启动时立即读取,并会导致发送所有内部调试消息。

• To enable internal debugging from a configuration file, the debug attribute on the log4net configuration element can be set to the value true. • 要从配置文件启用内部调试,可以将 log4net 配置元素上的 debug 属性设置为值 true。 For example:例如:

... configuration ...

Using this method does require that your configuration file is located and loaded, otherwise the attribute will not be read.使用此方法确实需要定位并加载您的配置文件,否则将不会读取该属性。 If you need to debug the process of locating the configuration file then use one of the other methods for enabling debugging.如果您需要调试定位配置文件的过程,请使用其他方法之一来启用调试。

Internal debugging messages are witten to the console and to the System.Diagnostics.Trace system.内部调试消息将发送至控制台和 System.Diagnostics.Trace 系统。 If the application does not have a console the messages logged there will be lost.如果应用程序没有控制台,则记录在那里的消息将丢失。 Note that an application can redirect the console stream by setting the System.Console.Out.请注意,应用程序可以通过设置 System.Console.Out 来重定向控制台流。 The Trace system will by default send the message to an attached debugger (where the messages will appear in the output window).默认情况下,Trace 系统会将消息发送到附加的调试器(消息将出现在输出窗口中)。 If the process does not have a debugger attached then the messages are sent to the system debugger.如果进程没有附加调试器,则消息将发送到系统调试器。 A utility like DebugView from http://www.sysinternals.com may be used to capture these messages.可以使用来自http://www.sysinternals.com 的DebugView 等实用程序来捕获这些消息。

I had the same issue.我遇到过同样的问题。

For my fix, I had to remove为了我的修复,我不得不删除

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

from my assemblyInfo.cs , if you're calling log4net.Config.XmlConfigurator.ConfigureAndWatch() in code.从我的assemblyInfo.cs ,如果您在代码中调用log4net.Config.XmlConfigurator.ConfigureAndWatch()

Even i have also faced the same problem, When i'm searching and searching, finally got an issue which was causing.即使我也遇到了同样的问题,当我搜索和搜索时,终于遇到了一个问题。

****First:**** Add below code in app.config file ****第一:**** 在 app.config 文件中添加以下代码

 <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-3level %logger : %message%newline" />
      </layout>
    </appender>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="..\\..\\Test_Output\\testLogFile.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-3level %logger : %message%newline" />
      </layout>
    </appender>
    <root>      
      <level value="INFO" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="RollingFileAppender" />
    </root>
</log4net>

****Second:**** You need to mention where you log4net located and Watch should be true ****第二:**** 你需要提到你log4net所在的位置,Watch应该是真的

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

****Finally:**** You need to mention in your BaseClass or Class where you wanted to Log it: XmlConfigurator.Configure(); ****最后:**** 你需要在你的 BaseClass 或 Class 中提到你想记录它的位置:XmlConfigurator.Configure();

***Hope's it will Work. ***希望它会奏效。

In the end to make it work I had to use the Extension of log4net.最后为了让它工作,我不得不使用 log4net 的扩展。

Get the Nuget package:获取 Nuget package:

Microsoft.Extensions.Logging.Log4Net.AspNetCore Microsoft.Extensions.Logging.Log4Net.AspNetCore

Then add this to program.cs:然后将其添加到 program.cs:

builder.Logging.AddLog4Net();

I left this code, because I need this in other processes like the backgroundworker:我留下了这段代码,因为我在后台工作人员等其他进程中需要它:

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

In your config file make sure that immediateFlush is set to true.在您的配置文件中,确保 immediateFlush 设置为 true。

 <immediateFlush value="false"/>

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

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