开发者

Configuring Appenders

I have the following situation.

There are 2 classes: ClassA, ClassB. I want them to log in different files ClassA.log, ClassB.log. For this purpose I am using the RollingFileAppender. How can I write this in a shorter way than the following:

<appender name="RollingFileAppenderA" type="log4net.Appender.RollingFileAppender">
    <file value="ClassA.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="100KB" />
    <staticLogFileName value="true" />
    <layout type开发者_StackOverflow="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

<appender name="RollingFileAppenderB" type="log4net.Appender.RollingFileAppender">
  <file value="ClassB.log" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="100KB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>

As you can see, the only thing the appenders differ is the filename where they are writing logs. So, is there a way to define a single RollingFileAppender for the given problem?


I have worked on several projects with multiple file appenders configured just like yours. To my knowledge there is no way to define a common appender configuration and then share that among multiple appenders. It probably has something to do with how log4net interprets the XML tags into Properties on the appenders. (And I have looked closely at the ForwardingAppender, but it's not built for that).


Is there any reason other than "don't repeat yourself" (DRY) that you want to clean up the configuration? If your app.config (or web.config) is getting out of hand, you can use a separate configuration file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜