VitalyB
VitalyB

Reputation: 12855

How do I roll on max files with a log4net RollingFileAppender that uses a parameterized name?

I have a client-side application that uses log4net's RollingFileAppender and that can be instantiated multiple times. Initially I've written all my logs into a single file, however, I've realized soon enough that log4net locks the file while writing, though, even if I used a less restrictive writing mode, I would still end up with a lot of mess in my log files.

I've decided to incorporate process-id into the file name, like so:

<appender name="HumanRollingLog" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="Log\TestLog[%processid].txt"/>
  <param name="DatePattern" value="dd.MM.yyyy'.log'"/>
  <appendToFile value="true"/>
  <rollingStyle value="Size"/>
  <staticLogFileName value="true" />
  <maxSizeRollBackups value="10"/>
  <maximumFileSize value="1KB"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%type] [%thread] %-5level %logger - %message%newline%exception%"/>
  </layout>
</appender> 

That worked. However, it completely messed up the rolling features since now every process spawns its own log file, the actual rolling would happen only after process ids start repeating. E.g, starting my application 3 times resulting the following logs being created:

TestLog[5396].txt
TestLog[5396].txt.1
TestLog[5396].txt.10
TestLog[5396].txt.2
TestLog[5396].txt.3
TestLog[5396].txt.4
TestLog[5396].txt.5
TestLog[5396].txt.6
TestLog[5396].txt.7
TestLog[5396].txt.8
TestLog[5396].txt.9
TestLog[5976].txt
TestLog[5976].txt.1
TestLog[5976].txt.10
TestLog[5976].txt.2
TestLog[5976].txt.3
TestLog[5976].txt.4
TestLog[5976].txt.5
TestLog[5976].txt.6
TestLog[5976].txt.7
TestLog[5976].txt.8
TestLog[5976].txt.9
TestLog[6860].txt
TestLog[6860].txt.1
TestLog[6860].txt.10
TestLog[6860].txt.2
TestLog[6860].txt.3
TestLog[6860].txt.4
TestLog[6860].txt.5
TestLog[6860].txt.6
TestLog[6860].txt.7
TestLog[6860].txt.8
TestLog[6860].txt.9

Anyone has an idea what can I do to resolve this issue? I'd like to have each process its own file, but I can't allow the rolling to be reused among ALL the processes.

Thanks!

Upvotes: 2

Views: 686

Answers (1)

Anthony Mastrean
Anthony Mastrean

Reputation: 22404

If you insist on using a process identifier in the name of the log file, then the built-in rolling patterns will never work. I would like to explore your requirements. What does "I would still end up with a lot of mess in my log files" really mean? What answers are you trying to get from your log files?

A solution to a different problem is to append the process id to the log messages and filter/search with one of the many tools available (log4net dashboard, log4net viewer, Apache Chainsaw, Microsoft LogParser, or Kiwi LogViewer.

Upvotes: 1

Related Questions