以编程方式将自定义属性添加到LoggingEventData

以编程方式将自定义属性添加到LoggingEventData

本文介绍了以编程方式将自定义属性添加到LoggingEventData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为log4net创建一个自定义包装器.我正在尝试填充LoggingEventData对象,并且有一些自定义属性要传递给Appender的append方法.

I am creating a custom wrapper for log4net. I am trying to fill my LoggingEventData object and there are some custom properties I want to pass to the Appender's append Method.

但是,这些属性是中间属性,因此我不希望它们出现在appenders配置中.

However, this these properties are intermediate properties, so I don't want them to be in the appenders configuration.

LoggingEventData.Properties是只读字典. 是否有一种方法可以让我在其中通过自定义属性(通过LoggingEventData)将属性传递给附加程序,而不必更新附加程序或配置文件?

LoggingEventData.Properties is a read only dictionary. Is there a way get a value in there for me to pass a custom property (via LoggingEventData) to the appender WITHOUT having to update the appender or a config file?

推荐答案

目前尚不清楚您要做什么,我怀疑无论它是什么,除非您可以使用LoggingEventData路径,否则它可能不是最好的在包装器中使用LoggingEventData实例的LoggingEvent构造器.

It's not really clear what you want to do, and I suspect that whatever it it is, the LoggingEventData path may not be the best, unless you can use the LoggingEvent constructor that takes a LoggingEventData instance in your wrapper.

可能有用的是,您可以轻松地将数据存储在log4net上下文中,并在登录时进行检索.

What may be of use is that you can easily store data in the log4net context and retrieve it when logging.

您可以通过以下方式在日志代码中对其进行设置:

You set it in your logging code thus:

 // Other contexts are available
 // http://logging.apache.org/log4net/release/manual/contexts.html
 log4net.GlobalContext.Properties["value1"] = "Value 1";

并以附加程序模式检索它,例如:

And retrieve it in the appender pattern, e.g.:

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%value1 (%property{value1})" />
  </layout>
</appender>

此有用的博客文章关于这个主题:

这篇关于以编程方式将自定义属性添加到LoggingEventData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:45