Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

asp.net mvc - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:estcommon.config"));

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

<appSettings>
  <add key="lo4gnet.Config" value="C:estcommon.config"/>
</appSettings>

Configure in the code:

log4net.Config.XmlConfigurator.Configure(new FileInfo(ConfigurationManager.AppSettings["lo4gnet.Config"]));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...