为单独的线程动态加载 log4net.Config ? C#/.NET

发布于 2024-10-19 13:36:17 字数 232 浏览 3 评论 0 原文

我的应用程序有许多无限期运行的并行单独线程。我正在使用 log4net 来记录信息。

我想动态检测 log4net 的日志配置信息。 (即:其中一个线程正在做一些奇怪的事情,我可以动态“注入”更详细的配置)。 我正在使用 log4net.Config 方法在其中一个线程中动态加载 XML 配置文件。不幸的是,它似乎影响了所有其他线程。

使用 log4net 时有没有办法隔离每个线程的配置?

谢谢你!

My application has a number of parallel separate threads running indefinitely. I'm using log4net for logging of information.

I'd like to instrument the logging configuration information for log4net dynamically. (ie: one of the threads is doing something weird, I can "inject" a more verbose configuration on the fly into it).
I'm using the log4net.Config method to load an XML configuration file on the fly in one of the threads. Unfortunately, it seems to impact every other thread.

Is there a way to isolate configurations for each thread when using log4net?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独﹏钓一江月 2024-10-26 13:36:17

使用 LogManager.GetLogger("MyApp.Thread1") 为将运行的每个线程创建一个新记录器。此方法可以接受一个字符串,因此只需为每个字符串指定一个不同的名称即可。然后您应该能够单独使用每个级别的日志级别。

 <!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
    <level value="DEBUG" />
    <appender-ref ref="A1" />
</root>

<!-- Print only messages of level WARN or above in the package Com.Foo -->
<logger name="MyApp.Thread1">
    <level value="WARN" />
</logger>

更多信息:
http://logging.apache.org/log4net/release/manual/configuration.html

另外,如果您刚刚开始,请查看 Log4View 。我没有任何隶属关系,只是一个满意的客户。

Create a new logger for each thread that will be running, using LogManager.GetLogger("MyApp.Thread1"). This method can take a string, so just give each one a different name. Then you should be able to use the log level for each one individually.

 <!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
    <level value="DEBUG" />
    <appender-ref ref="A1" />
</root>

<!-- Print only messages of level WARN or above in the package Com.Foo -->
<logger name="MyApp.Thread1">
    <level value="WARN" />
</logger>

More information:
http://logging.apache.org/log4net/release/manual/configuration.html

Also, if you're just getting started, check out Log4View . I don't have any affiliation, just a satisfied customer.

淡淡の花香 2024-10-26 13:36:17

我自己才刚刚开始使用 log4net,所以如果这没有用,我深表歉意,但我认为这个问题可以使用 上下文 功能。

您可以通过组合使用上下文过滤器来获取特定线程:

编辑< /strong>:

仔细看看这些,似乎 Mdc/Ndc 是遗留功能,所以也许这不是最好的选择。所以,这是另一个想法。

根据您识别线程的方式(如果您没有从线程池中处理完全黑盒任务),您可以简单地命名您的记录器,并使用过滤器来匹配记录器名称。

http://logging.apache.org/log4net/release/sdk /log4net.Filter.LoggerMatchFilter.html

只要您以自动重新加载的方式加载配置,您就可以在运行时配置过滤器:配置属性(特别是 Watch = true)。

您还可以使用 if(log.IsDebugEnabled) { /* dologging here */ } 等块在不记录日志时保持性能。

I've only barely started using log4net myself, so I apologize if this isn't useful, but I think this problem may be solvable using the Contexts feature.

You may be able to get your specific thread via a combination of that, and using a context filter:

Edit:

Looking at these closer, it seems that Mdc/Ndc are legacy features, so maybe this isn't the best option. So, here's another idea.

Depending on how you can identify your threads (if you aren't farming totally black-box tasks out of a thread pool), you might simply name your loggers, and use a filter to match on the logger name.

http://logging.apache.org/log4net/release/sdk/log4net.Filter.LoggerMatchFilter.html

You can configure the filters at runtime as long as you loaded your config in a way that it will auto-reload: Configuration Attributes (in particular, Watch = true).

You can also keep your perf up when not logging by using blocks like if(log.IsDebugEnabled) { /* do logging here */ }.

拥有 2024-10-26 13:36:17

“不幸的是,它似乎影响了所有其他线程”是什么意思?

我正在考虑性能或锁定。

如果您的线程被锁定(即第一个线程记录日志,但另一个线程挂起日志记录调用),那么您需要 FileAppenderMinimalLocking 选项。这样,一旦记录器写入日志文件,它就会将文件句柄释放给其他线程。不幸的是,这会影响性能(但性能仍然取决于内核:如果 Mac 或 Linux 内核,当您进入单声道时,在处理打开/关闭操作时更快或更慢,您会得到不同的结果),因为连续关闭/刷新/打开。

最终的解决方案是声明不同的附加程序不同的记录器。对于您实例化的每个记录器(之前了解它们!),声明一个专用的 FileAppender 供独占使用,这样线程就不会出现并发问题。

What do you mean by "Unfortunately, it seems to impact every other thread"?

I'm thinking about performance or locking.

If your threads get locked (ie. the first thread logs but the other hang in the logging call) then you need MinimalLocking option for FileAppender. This way, once a logger writes to the log file, it releases the file handle to other threads. Unfortunately, this affects performance (but the performance still depends on the kernel: if Mac or Linux kernels, when you go to mono, are faster or slower in handling open/close operations, you get different results) because of continuous close/flush/open.

Final solution is to declare different appenders and different loggers. For each logger you instantiate (know them before!), declare a dedicated FileAppender for exclusive usage, so threads won't go into concurrency issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文