Programmatically configuring log4net to write the same messages to two separate files

StackOverflow https://stackoverflow.com/questions/1415535

  •  06-07-2019
  •  | 
  •  

Question

Right now, I'm programmatically configuring log4net by creating a RollingFileAppender instance then calling BasicConfigurator.Configure(). This is working great, but I'm trying to create two identical files - one in a timestamped directory that gets created for every run of the app, another in a static location to make it easy for a developer to tail the most current log. (this is only used for running automated tests, so creating a huge pile of directories is not a concern)

Ideally, I'd like the files to be completely identical, but I realize log4net might not make this possible. If they have different logger names, that's fine - as long as I can have one log statement write to both log files.

My current code:

IAppender appender = new RollingFileAppender
{
    ...
}

appender.ActivateOptions();

BasicConfigurator.Configure(appender);
Was it helpful?

Solution

Although it's perfectly possible to do it completely Programmatically I don't feel like explaining the API here, it's easy enough to lookup.

By far the easiest and most flexiable way would be to configure two appenders read it from the config file. You can force this by calling upon log4net.Config.XmlConfigurator.Configure() and you can even pass in a XmlElement into Configure() if you want to keep it all in code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top