0

I am trying to add some custom fields in my log using c#. I am able to do that when I know about all the fields.

Now, in one scenario I don't know the number of fields. E.g. I have to add the parameters of the object and the numbers of object will keep on changing during run-time and I need to add the fields depending upon the number of objects.

Is there any tweak can be done in log4net to accomplish this so whenever there is a new Object the new field get created.

I am not sure mainly how to handle this in the config file for log4net.

1 Answer 1

1

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
         }
    }
}
3
  • ok, but its matching my requirement i guess. Its like I want to add 5000 values whose names are dynamic and will set when the application run. In log4net.config, we generally put those names but is there any way i can put those dynamically as putting 5000 values is not feasible
    – NitinG
    Commented Oct 28, 2014 at 5:14
  • If the values are coming from the object you may want to override the ToString method (or some other common method, for example from the interface) that would dump the specific values for each type of object. Either log the method output, or create a custom appender that processes these objects in a custom way. See 2nd edit
    – samy
    Commented Oct 28, 2014 at 7:48
  • got it thanks a lot. I have override the PatternLayout class and pushed all those properties and values accordingly. It seems decent at the start but lets see how it moves as I need to implement much complex stuff in it.
    – NitinG
    Commented Oct 28, 2014 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.