问题
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.
回答1:
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.
回答2:
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> %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"]));
来源:https://stackoverflow.com/questions/20657999/share-log4net-configuration-across-multiple-projects