Sure. Here’s how you can create a custom dynamic field:
Put this in your application’s log4net setup:
// This is a way of creating a dynamic property (found in ActiveProperties.cs)
// To use it, put a reference to the property in the appender's property: %property{custom1}
// It isn't actually used for anything right now, but I wanted it left in as an example
log4net.GlobalContext.Properties["custom1"] = new Custom1();
log4net.GlobalContext.Properties["custom2"] = new Custom2();
log4net.GlobalContext.Properties["custom2"] = new Custom3();
And then add this class to your project:
// You can use this class to create an active property to be used in the log4net configuration
//
// In your application setup routine you would create the property:
// log4net.GlobalContext.Properties["sampler"] = new SampleProperty();
//
// Then in the log4net.xml file, a pattern might contain this reference: %property{sampler}
//
using System;
using System.Collections.Generic;
using System.Text;
namespace Genesis.Logging {
public static class ActiveProperties {
public static string custom1 = "custom1 value";
public static string custom2 = "custom2 value";
public static string custom3 = "custom3 value";
}
public class Custom1 {
public override string ToString() {
return ActiveProperties.custom1;
}
}
public class Custom2 {
public override string ToString() {
return ActiveProperties.custom2;
}
}
public class Custom3 {
public override string ToString() {
return ActiveProperties.custom3;
}
}
}
Then, in your InfoFormat method, just set the sampleProperty to whatever you passed in, just before you log the error.