0

I have a gui that invoke applicaions instance. Every application use log4net root logger. The root logger gets the appenders from app.config. Every app.config contains TCP appender - that i wrote. The problem starts when im trying to rise up more than one application instance because the duplicate ports in use by the tcp appender.

The log4net section in my app.config looks like:

<log4net>
   <root>
     <level value="All" />
     <appender-ref ref="TCPAppender" />
   </root>

   <appender name="TCPAppender" type="LogSender.TCPAppender,LogSender">
      <Ip value="127.0.0.1"/>
      <Port value="8183"/>
      <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%d %-5p [%t] %c - %m%n" />
      </layout>
   </appender>
</log4net>

Can I change the port from the code of the application instance invoker? Or there is other option?

croxy
  • 4,082
  • 9
  • 28
  • 46
Hajaj
  • 49
  • 1
  • 7
  • 1
    Check out this answer here: http://stackoverflow.com/questions/16336917/can-you-configure-log4net-in-code-instead-of-using-a-config-file – Vahlkron Jan 19 '16 at 13:59
  • Hi, thank you for the quick answer. But i wants to save the port in the appconfig so the my application invoker will recoganize the port, so they will be able to communicate. – Hajaj Jan 19 '16 at 14:24

1 Answers1

0

Yes, it's easy to modify appenders at runtime:

public static void SetPort(int port)
{
    Hierarchy logHierarchy = log4net.LogManager.GetRepository() as Hierarchy;

    if (logHierarchy == null) return; // not configured yet

    var appender = logHierarchy.GetAppenders().OfType<TCPAppender>().SingleOrDefault();

    if (appender != null)
    {
        appender.Port = port;
        appender.ActivateOptions();
    }
}
stuartd
  • 70,509
  • 14
  • 132
  • 163