Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Trace Listener Attributes

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

like image 848
War Avatar asked Jul 19 '12 11:07

War


2 Answers

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.

like image 143
MrUpsideDown Avatar answered Oct 21 '22 17:10

MrUpsideDown


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" };
}
like image 2
DanB Avatar answered Oct 21 '22 15:10

DanB