2

We are using Log4Net in an asp.net service and would like to have the log files to be in the following format:
Name.Date.ProcessId.log.N
e.g. Service.20150123-09.776243.log.0

The RollingFileAppender needs to be of composite style to create new log file if either the date or size constraint is reached.

It's easy enough to have the processid first and then the date. We have that currently, but would prefer to have the date first so its easier to sort first by date.

I've tried various configuration but the nearest I could come up with is
Name.yyyyMMdd-HH.ProcessId.HH.log.N

<appender name="ServiceLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString">
        <conversionPattern value="C:\Logs\Service.%date{yyyyMMdd-HH}.%processid" />
    </file>
    <param name="DatePattern"  value=".HH.lo\g" />
    <param name="AppendToFile" value="true" />
    <param name="RollingStyle" value="Composite" />
    <param name="StaticLogFileName" value="false" />
    <param name="MaximumFileSize" value="10MB" />
    <param name="CountDirection" value="1" />
    <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d{MM/dd/yyyy HH:mm:ss fff},%X{Method},%X{TransID},%X{UserID},%X{DeviceID},%X{PID},%X{TH1},%m%n"/>
    </layout>
  </appender>

I need to have some valid date pattern in the DatePattern for it to work. Can't have just ".lo\g" there.
Adding the %processid in the DatePattern doesn't work.
But it would be ideal that way as we currently have a new log file every hour (because of our current DatePattern yyyyMMdd-HH). Probably variables in DatePattern won't work.
Setting the RollingStyle to "Size" makes above pattern work, but then new log files would be created based on Size and not Size and Date and we need it to be "composite".

Salman S
  • 65
  • 6
  • 1
    not sure if I understand correctly, but did you try something like this: %date{yyyyMMdd}.%processid".%date{HH} – Stefan Egli Feb 09 '15 at 19:52

0 Answers0