This should work, this is a bit of work, and it is based on the answer to the kit here .
Before calling XmlConfigurator.Configure();
add
ConverterRegistry.AddConverter(typeof(InstancePatternString), typeof(InstancePatternStringConverter));
Then add the following classes to your solution:
public class InstancePatternString : PatternString { public InstancePatternString(string pattern): base(pattern) { } public override void ActivateOptions() { AddConverter("cs", typeof(InstancePatternConverter)); base.ActivateOptions(); } } public class InstancePatternConverter : PatternConverter { override protected void Convert(TextWriter writer, object state) { switch(Option) { case "instance": writer.Write(MyContext.Instance); break; } } } public class InstancePatternStringConverter : IConvertTo, IConvertFrom { public bool CanConvertFrom(Type sourceType) { return sourceType == typeof(string); } public bool CanConvertTo(Type targetType) { return typeof(string).IsAssignableFrom(targetType); } public object ConvertFrom(object source) { var pattern = source as string; if (pattern == null) throw ConversionNotSupportedException.Create(typeof(InstancePatternString), source); return new InstancePatternString(pattern); } public object ConvertTo(object source, Type targetType) { var pattern = source as PatternString; if (pattern == null || !CanConvertTo(targetType)) throw ConversionNotSupportedException.Create(targetType, source); return pattern.Format(); } }
Be sure to change MyContext.Instance
to a statically accessible property that represents your instance.
Finally, change your web.config:
<file value=????How to specify this????? />
in
<file type="ConsoleApp.InstancePatternString, ConsoleApp" value="%cs{instance}\Reporting.log" />
Where ConsoleApp is the assembly in which you added these classes. This will create log files in separate instance directories. i.e. 1 \ Reporting.log, 2 \ Reporting.log, etc.
The advantage of this approach is that adding future properties is quite simple and just requires adding a switch statement to use in any future log / location file names.
source share