简体   繁体   English

具有多线程代码执行的Log4Net C#日志记录问题

[英]Log4Net C# logging issue with multi-thread code execution

I am using Log4Net to log an exception in my .Net Windows service application. 我正在使用Log4Net在我的.Net Windows service应用程序中记录异常。

I want to mention that, my windows service application is running with multi-threading execution , which means that each task is being processed on each different thread and I have used Delegate - (BeginInvoke) pattern. 我想提一下,我的Windows服务应用程序运行multi-threading execution ,这意味着每个任务都在每个不同的线程上处理,我使用了Delegate - (BeginInvoke)模式。

Importantly, I am using dynamic properties to generate multiple log files dynamically with Log4Net based on application's different scenarios. 重要的是,我使用dynamic properties根据应用程序的不同场景使用Log4Net动态生成多个日志文件。

Now, on every logging/ exception scenarios (in C# method), I have used Logger.Log method to log the info/ exception with Log4Net . 现在,在每个logging/ exception场景中(在C#方法中),我使用Logger.Log方法使用Log4Net记录信息/异常。

Code (Dynamic file generation) 代码(动态文件生成)

GlobalContext.Properties[FileNameParameter] = DirectoryName + fileName;
LogManager.Info(logMessage);

Config Settings 配置设置

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="D:\Data\%property{FileName}_info.log"/>
</appender>

The problem is (I believe), due to multi-threaded code execution, I am getting following things with Log4Net which is weird. 问题是(我相信),由于多线程代码执行,我正在跟踪Log4Net的事情,这很奇怪。

  1. Sometime log file generates, but NO message content. 有时会生成日志文件,但没有消息内容。
  2. Sometime file itself not generating. 有时文件本身没有生成。
  3. Sometime few file generates, few are NOT generating. 有时几个文件生成,很少生成。

Would you please do let me know why Log4Net behaves like this. 请你告诉我为什么Log4Net会像这样。 I need stable logging with multi-threading code execution. 我需要使用多线程代码执行的稳定日志记录。

Thanks in advance! 提前致谢!

I haven't actually tried to do what you are doing, but I can see two problems with your code which would cause problems. 我实际上并没有尝试做你正在做的事情,但我可以看到你的代码有两个问题,这会导致问题。 (I wouldn't expect logging information to be lost, but I would expect the information to go to the wrong file). (我不希望日志信息丢失,但我希望信息转到错误的文件)。

Firstly, you should be using ThreadContext rather than GlobalContext if the property is expected to change on different threads. 首先,如果期望在不同的线程上更改属性,则应该使用ThreadContext而不是GlobalContext。 That issue is trivial to fix. 这个问题很容易解决。

The second issue may be more problematic. 第二个问题可能更成问题。 Log4net does not expect the base filename to change during program execution and may continue to write to the old filename even after you have changed your property. Log4net不希望在程序执行期间更改基本文件名,即使您更改了属性,也可能继续写入旧文件名。

One of the easiest ways around this is to re-initialize your logger. 解决此问题的最简单方法之一是重新初始化记录器。 If you are using xml configuration from your app.config, you can call log4net.Config.XmlConfigurator.Configure(); 如果您使用app.config中的xml配置,则可以调用log4net.Config.XmlConfigurator.Configure(); after you have set your property. 在您设置了您的财产之后。 This obviously has an overhead and you have to decide whether this overhead is acceptable. 这显然有一个开销,你必须决定这个开销是否可以接受。

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

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