Out of the box, dynamically changing appenders is hard to do. I would consider logging to a different medium, like a database table, that enables filtering and grouping log data.
Update:
Using contexts you can provide per-instance information across instance boundaries. Consider the following implementations:
class ComponentA
{
public void DoStuff()
{
using(log4net.ThreadContext.Stacks["component"].Push(this.Id))
{
childComponent.DoStuff();
}
}
}
class ChildComponent
{
public void DoStuff()
{
using(log4net.ThreadContext.Stacks["component"].Push(this.Id))
{
childChild.DoStuff();
}
}
}
Whenever messages are logged (regardless of which component does the logging), the current context stacks are included. In your appender configuration, use the %property{component}
formatter to output the content of the component stack which will include ids of both the ComponentA
instance and the ChildComponent
instance.