Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass arguments to a custom log4net Appender's constructor?

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

As for version 1.2.10, this is not possible without modifying the source code. The relevant section is in Repository\Hierarchy\XmlHierarchyConfigurator.cs:L286:

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

As you can see, it should use this overload (or something along that way) to allow me to achieve my needs.

Activator.CreateInstance(Type, Object[])
like image 627
Simone Avatar asked Oct 15 '10 10:10

Simone


2 Answers

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.

like image 168
Stefan Egli Avatar answered Oct 20 '22 16:10

Stefan Egli


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.
like image 45
Simone Avatar answered Oct 20 '22 15:10

Simone