How to programmatically get the log4net log name template?

I am trying to write C # code that retrieves a runtime template for the log4net log file name through the log4net API.

That is, if the following appender is defined in log4net.config:

<appender name="MyAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="%date{yyyy}\%date{MM}\%date{dd}\%property{Id}.log" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="16" /> <maximumFileSize value="1MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message%newline" /> </layout> </appender> 

I would like to get% date {yyyy} \% date {MM} \% date {dd} \% property {Id} .log in a string variable in the code (indiscriminately log4net.config as plain XML).

Does anyone have an idea ho to pull this trick?

Thanks in advance.

+7
source share
4 answers

The value of this template will be in the File property of the FileAppender instance of that part of your registration repository.

Getting in the application can be done in several ways.

  • If you already have an instance of ILogger (for example, what you get from LogManager.GetLogger() ), then it has the Appenders property. This will give you any applications associated with this particular level of registration hierarchy.
  • You can also call LogManager.GetRepository() to get a Heirarchy object that contains the entire registrar and appender hierarchy. The method expects assembly, so go to GetCallingAssembly() to get the default value. This class has a GetAppenders() method that returns all configured appenders, although it will not tell you which of them are associated with registrars.

From there, just browse the search for the appropriate type (for example, FileAppender or RollingFileAppender ), then read its File property.

+2
source

I think you want to subclass RollingFileAppender and use your subclass in the configuration file instead of the base class. Be sure to include a class name with a full namespace so that log4net can find it.

I have only UDPAppender subclassed in my code. I added an override as follows:

  public override void ActivateOptions() { base.ActivateOptions(); } 

When I crashed before calling the base class, I looked at 'this' in the LAN window and saw the values ​​for UDPAppender members.

0
source

Michael Edenfield's explanation is good. Here's the implementation.

 private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); string fileName = ((RollingFileAppender)log.Logger.Repository.GetCurrentLoggers() .Where(e => e.Name == "Your namespace.class").ToList()[0] .Repository.GetAppenders() .Where(e => e.Name == "MyAppender").ToList()[0]).File.ToString(); 
0
source

I can get the <file value> with the following code:

 ((log4net.Appender.FileAppender) ((log4net.Appender.IAppender[]) ( (log4net.Repository.Hierarchy.Logger) log.Logger).Appenders.SyncRoot)[0]).File 
0
source

All Articles