User User - 23 days ago 15
C# Question

How can I get the log filename through code?

How can I get the log filename in code? (So I can print it to the console)

My log file is define like this in app.config:

<appender name="RollingLogFileAppender"
type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
...

Answer

You look at your LogManager.GetAllRepositories. Inside the repositories you look at all the appenders in ILoggerRepository.GetAppenders. If the appender is of type RollingFileAppender you check its File property, or if you preffer you can check the appender by Name.

That being said it is very bad to do something like this in your code. What if the configuration is changed at deployment and the appender you expect is no longer present?