Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share log4net configuration across multiple projects

In my WCF solution, I have multiple projects(16) under this solution. (Business Objects, Business Layer, Windows services etc.)

I need to integrate log4net logging framework to all projects. What would be the best approach to have a one log4net config file shared across all the projects.

like image 798
Dhanuka777 Avatar asked Dec 20 '22 20:12

Dhanuka777


2 Answers

Visual Studio allows you to link files from the solution level down to the project level. I would use this approach over the approach of hard coding at path.

Add > Existing Item > Add As Link

See the image for clarity.

Add As Link

like image 116
Khalid Abuhakmeh Avatar answered Jan 11 '23 11:01

Khalid Abuhakmeh


If you only have one start up project that is ran (e.g. only one of them is an MVC project, where the rest are simple libraries), you only are required to have the config in the MVC project. In each library, you will still be able to use the MVC project's configuration.

However, if you have multiple start up projects, you can configure log4net to look at a separate, shared config file outside of the app.config. Example:

<log4net>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date{hh:mm:ss tt} %level&gt; %message%newline"/>
    </layout>
  </appender>
  <root>
    <level value="DEBUG"/>
    <appender-ref ref="ConsoleAppender"/>
  </root>
</log4net>

In the projects:

log4net.Config.XmlConfigurator.Configure(new FileInfo(@"C:\test\common.config"));

You can include an app setting as well so this isn't compile specific:

<appSettings>
  <add key="lo4gnet.Config" value="C:\test\common.config"/>
</appSettings>

Configure in the code:

log4net.Config.XmlConfigurator.Configure(new FileInfo(ConfigurationManager.AppSettings["lo4gnet.Config"]));
like image 40
matth Avatar answered Jan 11 '23 09:01

matth