You can add the properties of your object in a custom property for log4net and log the content of the property in your formatting:
For example in your code:
log4net.ThreadContext.Properties[ "myObjectProperties" ] = obj.prop1 + " " obj.prop2; // + ...;
and in the configuration:
<conversionPattern value="%logger (%property{myObjectProperties}) [%level]- %message%newline" />
You cannot have a pattern that is configurable on the fly; you could have multiple patterns that would match different objects but this won't be very easy to manage.
EDIT: well, you could have a runtime configurable pattern but not natively :) However you could perhaps have a pattern that is able to be loaded from the aforementioned properties
2nd EDIT: If there are as much as 4000 properties you need, why not consider either pushing all those properties as part of the message itself (log.Info(myObject.ToString())
) or create a custom appender that would be able to handle a specific interface to process:
public interface IHaveManyFieldsToLog
{
public string[] GetAllPropertyValues()
}
public class ManyFieldsToLogAppender: SkeletonAppender
{
// pseudocode, I don't have the IDE at the moment
public override AppendLog(LogEvent event)
{
if (event.Parameter[0] as IHaveManyFieldsToLog != null)
{
var values = (event.Parameter[0] as IHaveManyFieldsToLog).GetAllPropertyValues();
// concat all values and push it to the log
}
}
}