2

This code works as expected in Log4net: <file type="log4net.Util.PatternString" value=".\\somefolder\\myLog.%property{logId}.log"/>

Adding thie following C# code the logfile "myLog.2.log" is created successfully

log4net.GlobalContext.Properties["logId"] = 2;
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(log4NetConfigFilePath));

Here comes the problem:

Using Variables like maxSizeRollBackups, maxFileSize or maximumFileSize for RollingFileAppender doesn't work for me. Depending on the Property different errors occur:

1) maximumFileSize :

C#

log4net.GlobalContext.Properties["maxfilesize"] = 15;

log4net.config:

<maximumFileSize value="%property{maxfilesize}MB" />

I get the error:

log4net:ERROR OptionConverter: [%PROPERTY{MAXFILESIZE}] is not in the correct file size syntax.

2) maxSizeRollBackups:

C#:

log4net.GlobalContext.Properties["maxrollbackups"] = 5;

Log4net.config:

<maxSizeRollBackups value="%property{maxrollbackups}" />

I get the error:

log4net:ERROR Could not create Appender [RollingLogFileAppender] of type [dotnet.CustomFileAppender]. Reported error follows. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) --- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at log4net.Util.OptionConverter.ConvertStringTo(Type target, String txt) at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Type type, String value) at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element, Object target) at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement) log4net:ERROR Appender named [RollingLogFileAppender] not found.

Additional Info:

  • The RollingFileAppender works fine as long as I use fixed values in the config.
  • For the maxFileSize property also the 2) error occurs.
  • Using both Properties maxSizeRollBackups & maximumFileSize with variables at the same time, ends up in the second error
  • Docu regarding MaximumFileSize vs MaxFileSize
Haphil
  • 1,180
  • 1
  • 14
  • 33

0 Answers0