14

I have a third party using a configuration file that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    <!--Others sections-->
  </configSections>

  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value=".\logs\logclient.txt" />
      <appendToFile value="false" />
      <rollingStyle value="Date" />
      <maximumFileSize value="1000KB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date  [%thread] %-5level %logger [%ndc] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>

</configuration>

The code in the third party looks like :

LogManager.GetRepository(Assembly.GetCallingAssembly()), configFile);

LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

I would like the third party dll to use my own appender defined in my own configuration file. How can I managed this ?

NB :

  • the third party need to use its own configuration file because others sections are mandatory and I can not add them in my file
  • I can modify the third party configuration file, I can not modify mine
Toto
  • 7,491
  • 18
  • 50
  • 72
  • DLLs use the app.config of whatever EXE is hosting them. So if you load the DLL, it is using your app.config. So I am confused when you say "the third party needs to use its own configuration file." .NET app.configs don't work that way. – Moby Disk May 26 '15 at 13:43
  • Actually I have two configuration files : one app.config (use by my code) and one thirdParty.dll.config (used by the third party). I don't know if relevant but the thirdParty.dll is not in the same folder as my application and it is loaded manually and used by reflection. – Toto May 26 '15 at 13:54
  • Instead of trying to force it to use the appender defined in your own config, why not just copy and paste it into their config, and voila...? – johnjps111 May 29 '15 at 14:15
  • @JohnnyStrings i really hate duplication :) I would need to do the copy/paste at execution time because i don't want to maintain the two config files, also main log4net file config is pretty big (1000+ line) and may "hide" the relevant configuration for the third party config. Thought this should work :) – Toto May 30 '15 at 05:20
  • Would deployment time be as good as execution time? If so, perhaps an automated deployment could do the copy for you? – johnjps111 May 31 '15 at 04:28

4 Answers4

2

There are two existing questions that propose a solution to dynamically edit log4net configuration:

Community
  • 1
  • 1
Arnaud Develay
  • 3,920
  • 2
  • 15
  • 27
2

As far as I understand, you must use the third party configuration file and you can modify it.

The configSource property/attribute should work to redirect the log4net configuration section of your third party configuration file.

In the third party configuration file :

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>

<log4net configSource="pathtoyourlog4net.config" />
JoeBilly
  • 3,057
  • 29
  • 35
0

If your .NET app is referencing the thirdparty.dll, at runtime the app will ignore the thirdparty.dll.config completely. Also, the thirdparty.dll will not pickup the settings in the thirdparty.dll.config as it is expecting these to be loaded by the exe.

What you have to do is to merge (or replicate) the content of the thirdparty.dll.config into your own app.config. Both your app and the thirdparty.dll will be able to pickup the settings in your app.exe.config and you only need to maintain one .config file.

Furthermore, you can then add additional appenders to the log4net section and it will be picked-up by both your app and the thirdparty.dll.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Tien Dinh
  • 1,037
  • 7
  • 12
  • Third party is manually loading the log4net config from a file they expect to be of the form ThirdPartyDllPath.dll.config – Toto May 30 '15 at 05:19
0

log4net library is a tool to help the programmer output log statements to a variety of output targets.

You can also configure log4net in code instead of using a config file,this SO post clearly explains the question.

Change appender dynamically:

public static void ChangeFilePath(string appenderName, string newFilename)
        {                       
            log4net.Repository.ILoggerRepository repository = log4net.LogManager.GetRepository();
            foreach (log4net.Appender.IAppender appender in repository.GetAppenders())
            {
                if (appender.Name.CompareTo(appenderName) == 0 && appender is log4net.Appender.FileAppender)
                {
                    log4net.Appender.FileAppender fileAppender = (log4net.Appender.FileAppender)appender;
                    fileAppender.File = System.IO.Path.Combine(fileAppender.File, newFilename);
                    fileAppender.ActivateOptions();                   
                }
            }           
        }

Reference post

Community
  • 1
  • 1
Tharif
  • 13,794
  • 9
  • 55
  • 77