You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Trent Cioran <tr...@gmail.com> on 2014/02/04 23:17:15 UTC

Logging repositories

Hello All,

I have an advanced scenario for logging:

1) - I have an application which logs to default repository
2) - A manager thread that acts as a coordinator for the work to be done,
this component should log to its own log file
3) - A processing thread that performs the actual work, this component will
process X number of items so I want to log the component's activity to its
own log so it is easier to troubleshoot in case of error and not mix with
other processing threads output.
4) - An error log which should generate the detail for a specific
processing item error

Currently I have two appenders configured with filters so one logs to file
DEBUG and INFO and the other logs only ERROR.

I am using a pattern string to specify the name of the log file like this:
Processor_Mbx_%property{mbxId}_Seq_%property{seq}_%date{yyyyMMdd}.txt

I am using repositories in order to keep them separated and also to specify
different configuration for each repository (for example manager thread
only requires ,bxId and seq variables for the log file name while the item
error requires these two and a third itemId. I am configuring my
repositories using

                var repository = LogManager.
CreateRepository(repositoryName);

                XmlConfigurator.ConfigureAndWatch(repository,
configFileInfo); // See sample at the bottom



These properties are set using the ThreadContext so each thread do not
collide with the others. Log4net is generating the files as expected
everything nice here, but the log goes to the first file that it created,
for example, it creates files

Processor_Mbx_1012_Seq_1_20140204.txt <--- logs are sent to this file
Processor_Mbx_1013_Seq_1_20140204.txt


I already tried using ThreadContext.Properties and ThreadContext.Stack but
I get the same behaviour


Hope someone has an idea of why I am getting this behaviour and what I need
to do to get the result I want.


Thanks in advance,
*TrentCioran*


----------------

Sample configuration file:


<?xml version="1.0" encoding="utf-8" ?>

<log4net>

  <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">

    <file type="log4net.Util.PatternString" value="
C:\Logs\InfoLogs\Processor_Mbx_%property{mbxId}_%date{yyyyMMdd}.txt"/>

    <staticLogFileName value="true"/>

    <param name="appendToFile" value="true"/>

    <param name="maximumFileSize" value="10MB"/>

    <param name="maxSizeRollBackups" value="1000"/>

    <layout type="log4net.Layout.PatternLayout">

      <conversionPattern value="
%date|[%thread]|%-5level|%logger|%property{NDC}|%message%newline"/>

    </layout>

    <filter type="log4net.Filter.LevelMatchFilter">

      <levelToMatch value="DEBUG"/>

    </filter>

    <filter type="log4net.Filter.DenyAllFilter"/>

  </appender>

  <appender name="errorAppender" type="log4net.Appender.RollingFileAppender
">

    <threshold value="ERROR"/>

    <file type="log4net.Util.PatternString" value="
C:\Logs\ErrorLogs\Processor_Mbx_%property{mbxId}_%date{yyyyMMdd}.txt"/>

    <staticLogFileName value="true"/>

    <param name="maximumFileSize" value="10MB"/>

    <param name="maxSizeRollBackups" value="1000"/>

    <layout type="log4net.Layout.PatternLayout">

      <conversionPattern value="
%date|[%thread]|%-5level|%logger|%property{NDC}|%message%newline"/>

    </layout>

    <filter type="log4net.Filter.LevelMatchFilter">

      <levelToMatch value="ERROR"/>

    </filter>

    <filter type="log4net.Filter.DenyAllFilter"/>

  </appender>

  <root>

    <level value="DEBUG" />

    <appender-ref ref="errorAppender" />

    <appender-ref ref="infoAppender" />

  </root>

</log4net>