Search code examples
c#log4netlog4net-configuration

How can i get a value of log4net tag at app.config?


I'm making a program, that using log4net for loging it. I added library at the project, configured app.config file as i need and it works great, but how can i get information from log4net tags for using it at my code ? For example, this is part of my app.config file:

<log4net>
  <appender name="LogMainDebug" type="log4net.Appender.FileAppender">
    <param name="File" value="D:\Folder\log-mainDebug.txt" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <param name="AppendToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="[%date{dd-MM-yyyy HH:mm:ss}] [%-5p] - [%m]%n" />
    </layout>
  </appender>  

<root>
  <appender-ref ref="LogMain" />
  <appender-ref ref="LogMainDebug"/>
</root>     

So, how can i get from it a path value "D:\Folder\log-mainDebug.txt" and use it at my code ?


Solution

  • The value you're after is a property of FileAppender. You simply need to get it from the current instance of the appender:

    var appenders = LogManager.GetRepository().GetAppenders();
    var filePath = appenders.OfType<FileAppender>().Single().File;