views:

34

answers:

2

Assume you are using some libraries like NHibernate or Castle ActiveRecord that use log4net internally. Your application uses log4net too. It's possible to configure all applications to save logs into file or any other output. But the problem is by enabling log4net for my own application, other programs save their log into the log file and causes it grow very fast with information that I don't need at the moment.

How can I route logs of each application to different outputs or at least how can I deny other applications from logging?

+2  A: 

NHibernate/Castle Active Record generate lot of log information but that is all DEBUG level logging. So you can turn down your log level from "ALL" to "INFO" or "ERROR" in config file and you should be OK.

log4Net also support named logger and logger hierarchy. I am sure both NHibernate/Castle would be using named logger. So you can choose to ignore that particular named logger using configuration. See log4Net help where they have used have different logging level for Com.Foo library.

Using named logger is a typical way of separating log traces from different components/modules/libraries etc. Each application (as in different process) would have different configuration file and you can always have different log files to separate the log traces.

VinayC
how can I determine NHibernate/Castle uses which logger?
afsharm
if you use the %logger property in your log output you will see it if you let the application run. Maybe you will see NHibernate.Sql or something like this. is sufficient to configure a logger NHibernate to not write any messages. For Castle it will be similiar...
Stefan Egli
@Stefan: Thank you really. They are something like NHibernate.Loader.Loader, NHibernate.Event.Default.DefaultLoadEventListener, NHibernate.AdoNet.AbstractBatcher, etc
afsharm
+1  A: 

Just direct different loggers to different appenders.

Pseudo example:

<log4net>
  <appender name="MyAppender" type="log4net.Appender.FileAppender">
  <!--appender properties (file name, layout, etc)-->
  </appender>
  <appender name="NHAppender" type="log4net.Appender.FileAppender">
  <!--ditto-->
  </appender>
  <logger name="MyAppMainNamespace">
    <level value="INFO"/>
    <appender-ref ref="MyAppender" />
  </logger>
  <logger name="NHibernate">
    <level value="ERROR"/>
    <appender-ref ref="NHAppender" />
  </logger>
</log4net>
Diego Mijelshon
@Diego: your solution works well. Many thanks.
afsharm