Loading Trace Listener Attributes
Asked Answered
E

2

5

So I have a custom trace listener which began it's life as this:

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

I have modified this to work more like the Log4Net RollingFileAppender (see: http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html)

When I run the code I find that it doesn't set the property / field values from the custom attributes in the config file.

Analysing the object at runtime reveals that the Attributes property (this.Attributes) contains nothing.

Any ideas how I would fix this?

Am I supposed to manually populate these or something?

Ok here's a code sample:

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

In the LoadAttributes method i then do ...

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

The problem is "Attributes" never contains anything. This class instantiated from framework code using the config information which does contain the attributes...

 <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>

Edit 2:

The XmlWriterTraceListener class is part of .Net, by making that my base class in Inherit the Attributes property.

In the config I should be able to specify any attribute then in the code do something like ...

var attValue = Attributes["something"];

... but for some reason this comes back null (the attribute is not in the collection).

Ezequieleziechiele answered 19/7, 2012 at 11:29 Comment(1)
Without the code that doesn't work and the config that it's suppose to work with; it's really hard to tell. Please provide the smallest possible example (do not copy and paste your current code) that reproduces the problem.Crofoot
D
7

The Attributes collection is not populated from the configuration file until after the instance of your listener is fully constructed.

Instead of calling your LoadAttributes method in the constructor, you need to make sure it is called after your listener object is completely instantiated, but before it is first used.

Dinghy answered 5/3, 2014 at 19:1 Comment(0)
S
2

Just in case you haven't fixed this or for anybody's future help...

I've been looking at this today. Did you include the override for GetSupportedAttributes?

For a while this morning I had the same problem. Including this and listing those attributes I'm expecting solved it. So in your example above you'd need

protected override string[] GetSupportedAttributes()
{
    return new[] { "MaxTraceFileSize" };
}
Sully answered 5/11, 2012 at 1:17 Comment(2)
Does merely adding that populate the value in some way? It's been a while since i looked at that code, i left the values hard coded in the end because i never did see it resolved but theres a bug fixing session coming up soon and this is on the list of things to do.Ezequieleziechiele
GetSupportedAttributes needs to be implemented to have .NET read the attribute values from the config file - but this is only half the story. The Attributes collection is always empty when accessed in the constructor of a custom TraceListener object.Dinghy

© 2022 - 2024 — McMap. All rights reserved.