所以我有一个自定义跟踪侦听器,它的生命开始时是这样的:
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:LogsMyTraceFileName.svclog"
MaxTraceFileSize="1048576"
MaxTraceFileCount="10"
/>
</sharedListeners>
编辑2:
XmlWriterTraceListener类是.Net的一部分,方法是使其成为Inherit The Attributes属性中的基类。
在配置中,我应该能够指定任何属性,然后在代码中执行以下操作。。。
var attValue = Attributes["something"];
但由于某种原因,它返回为null(属性不在集合中)。
属性集合不会从配置文件中填充,直到在之后完全构建了侦听器的实例。
您不需要在构造函数中调用LoadAttributes方法,而是需要确保在侦听器对象完全实例化之后,但在首次使用之前调用它。
万一你还没有解决这个问题或需要任何人的未来帮助。。。
我今天一直在看这个。是否包含GetSupportedAttributes的覆盖?
今天早上有一段时间我也遇到了同样的问题。包括这个并列出我希望解决的那些属性。所以在上面的例子中,你需要
protected override string[] GetSupportedAttributes()
{
return new[] { "MaxTraceFileSize" };
}