Custom LogFilter cannot instantiate

Topics: Logging Application Block
Mar 2, 2007 at 10:18 AM
Hi!

I am writing a Custom LogFilter called UserLogFilter. The Config looks like this:

<add name="Custom Filter"
type="<companyname>.Framework.Logging.UserLogFilter, <companyname>.Framework" />

The beginning of my implementaion type looks like this:

ConfigurationElementType(typeof(CustomLogFilterData))
public class UserLogFilter : LogFilter
{
public UserLogFilter(string name): base(name)
{
...
}

...
}

I get the problem when the ObjectBuilder tries to instantiate the type in the CustomProviderAssembler:

Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.CustomProviderAssembler<TObject, TConfiguration, TConcreteConfiguration> : IAssembler<TObject, TConfiguration>().Assemble(IBuilderContext, TConfiguration, IConfigurationSource, ConfigurationReflectionCache)

The line that reads:

TObject provider = (TObject)Activator.CreateInstance(objectConfiguration.Type, castedObjectConfiguration.Attributes);

Throws an exception due to missing constructor. The attributes contain 0 attributes and the type is the correct type. As I see it there should be one (1) attribute called "Name" - compare to the config above. And the constructor should be able to be called. I've also tried to add a parameterless constructor to my Custom LogFilter:

public UserLogFilter(string name): base("Custom Filter")
{
...
}

But that does not work either.

Can anyone shed some light on this issue?

Cheers,

/Magnus
Mar 2, 2007 at 1:54 PM
The constructor for your untyped custom log filter takes a NameValueCollection as a parameter:

public UserLogFilter(NameValueCollection values) : base(values["Name"])
{
  // Do Something With NameValueCollection
}

Regards,

Dave

____________________

David Hayden
Microsoft MVP C#
Mar 2, 2007 at 3:05 PM
That was it! Great thanks! I did not see that anywhere in the little documentation there is to this beta product.

Now it works like a charm...

/Magnus - NoopMan

http://blog.noop.se/
Mar 5, 2007 at 6:27 AM
You know what... I spoke too soon. If I change my constructor to a NameValueCollection the instance will get constructed but it holds no entries. The "name" property from my config is not included in the name val col. Since I only have one custom log filter I can ask for it by calling the Logger.GetFilter<UserLogFilter>() method without a name parameter. However In my constructor code I then have to call the base constructor using a null argument for name which means the filter will not be named in run-time and so I can't refer to it by name!

It works to use the non paremeterized version of the GetFilter method but it is not 100% correct since I cannot name the filters in runtime and not refer to them by name.

Is this a bug or a feature? Is the name supposed to be supplied in the NameValueCollection or is it not?


NoopMan wrote:
That was it! Great thanks! I did not see that anywhere in the little documentation there is to this beta product.

Now it works like a charm...

/Magnus - NoopMan

http://blog.noop.se/