User User - 1 year ago 109
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"
<file value="logfile" />

Answer Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download