views:

509

answers:

1

How can I get the log file name 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" />
    ...
+2  A: 

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?

Remus Rusanu
Well, I can check for null values. What about reading the app.config somehow?
User
see the example at http://msdn.microsoft.com/en-us/library/system.configuration.configuration.getsection.aspx on how to read your own config section(s). I'd recommend againts it, though.
Remus Rusanu
I'd like to print out to the console where the log file is for convenience. Any way to do this you would suggest?
User
My point is that log4net allow for *any* appender. If is a file appender is fine, you can search the LogManager and display the File property, as in my post. But what if is an appender *you have no idea exists*, eg. the appender my company provides at http://bugcollect.com/downloads ? Anyone can add such and appender to your application.config and remove your RollingFileAppender, so what are you going to print then?
Remus Rusanu