tags:

views:

22

answers:

2

Hello, is it possible to change the way Appenders are initialized?

I'd like to pass arguments to a custom appender's constructor so I guess I have to override Appenders' initialization mechanism. Problem is that I can't find, in the docs, a way to hook it up, and it makes me think that it's not possible (or that the docs are incomplete).

A: 

I am not sure if I understand your goal, but if you want your appender to be configurable you basically have to expose a property on your appender. Then you can either set this property either programmatically or in the configuration file.

The UdpAppender exposes a property like this:

public int LocalPort
{
   get; set;
}

(It is actually a bit more complex as they check if the value in the setter is a valid port.)

In the configuration file you use it like this:

<localPort value="8080" />

This works very well for simple types like string, int ... but also for some complex types like IPAddress. If you have your own type then it will be more difficult to make it work and I would have to check first how this is done.

Stefan Egli
Stefan, the problem is exactly the one you write at the end: I have to provide a complex object that it's instantiated at runtime. There's no way to provide it via XML configuration.
Simone
You cannot create an instance of this class based on some parameter strings?
Stefan Egli
A: 

As for version 1.2.10, this is not possible without modifying the source code.

The relevant section is in Repository\Hierarchy\XmlHierarchyConfigurator.cs at line 286:

`IAppender appender = (IAppender)Activator.CreateInstance(SystemInfo.GetTypeFromString(typeName, true, true));`

As you can see, it should use

Activator.CreateInstance(Type, Object[])
overload (or something along that way) to allow me to achieve my needs.

Simone