【问题标题】:Loading Trace Listener Attributes加载跟踪侦听器属性
【发布时间】:2012-07-19 11:29:37
【问题描述】:

所以我有一个自定义的跟踪监听器,它的生命是这样开始的:

http://www.codeproject.com/Articles/30956/A-Rolling-XmlWriterTraceListener

我已对其进行了修改,使其更像 Log4Net RollingFileAppender(请参阅:http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html

当我运行代码时,我发现它没有从配置文件中的自定义属性中设置属性/字段值。

在运行时分析对象会发现 Attributes 属性 (this.Attributes) 不包含任何内容。

有什么想法可以解决这个问题吗?

我应该手动填充这些吗?

好的,这是一个代码示例:

[HostProtection(Synchronization = true)]
public class RollingXmlWriterTraceListener : XmlWriterTraceListener
{
    public RollingXmlWriterTraceListener(string filename)
        : base(filename)
    {
        _basicTraceFileName = filename;
        LoadAttributes();
    }

然后在 LoadAttributes 方法中执行 ...

if (Attributes.ContainsKey("maxTraceFileCount"))
{
   string attributeValue = Attributes["maxTraceFileCount"];

问题是“属性”从不包含任何内容。 此类使用包含属性的配置信息从框架代码实例化...

 <sharedListeners>
    <add type="emedia.Common.Wcf.RollingXmlWriterTraceListener, emedia.Common.Wcf" 
         name="System.ServiceModel.XmlTrace.Listener" 
         traceOutputOptions="None" 
         initializeData="C:\Logs\MyTraceFileName.svclog" 
         MaxTraceFileSize="1048576"
         MaxTraceFileCount="10"
    />
 </sharedListeners>

编辑 2:

XmlWriterTraceListener 类是 .Net 的一部分,通过在继承 Attributes 属性中使其成为我的基类。

在配置中我应该能够指定任何属性,然后在代码中执行类似 ...

var attValue = Attributes["something"];

...但由于某种原因返回 null(该属性不在集合中)。

【问题讨论】:

  • 没有不起作用的代码和它应该使用的配置;真的很难说。请提供重现问题的最小可能示例(不要复制和粘贴您当前的代码)。

标签: c# configuration trace tracelistener


【解决方案1】:

直到你的监听器实例完全构建之后,属性集合才会从配置文件中填充。

您需要确保在侦听器对象完全实例化之后,但在首次使用之前调用它,而不是在构造函数中调用您的 LoadAttributes 方法。

【讨论】:

  • 谢谢,这正是我的问题!
【解决方案2】:

以防万一你还没有解决这个问题或任何人未来的帮助......

我今天一直在看这个。您是否包含了 GetSupportedAttributes 的覆盖?

今天早上有一段时间我遇到了同样的问题。包括这个并列出我期望解决的那些属性。所以在你上面的例子中,你需要

protected override string[] GetSupportedAttributes()
{
    return new[] { "MaxTraceFileSize" };
}

【讨论】:

  • 是否只是添加以某种方式填充值?自从我查看该代码以来已经有一段时间了,我最后留下了硬编码的值,因为我从来没有看到它被解决,但是很快就会有一个错误修复会话,这在要做的事情列表中。
  • GetSupportedAttributes 需要实现以使 .NET 从配置文件中读取属性值 - 但这只是故事的一半。在自定义 TraceListener 对象的构造函数中访问时,Attributes 集合始终为空。
猜你喜欢
  • 2017-09-24
  • 2012-05-12
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多