Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect log4net logs from third party

Tags:

c#

log4net

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
like image 632
Toto Avatar asked May 21 '15 18:05

Toto


2 Answers

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

  • Dynamically reconfigure Log4Net
  • How can I change the file location programmatically? (see JackAce answer).
like image 150
Arnaud Develay Avatar answered Oct 05 '22 19:10

Arnaud Develay


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" />
like image 30
JoeBilly Avatar answered Oct 05 '22 21:10

JoeBilly