log4net RollingFileAppender unexpected filename when file already open - log4net

My log4net configuration is:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\LogFiles\Staging_WebAQPro\s_waq_" />
<datePattern value="yyyy-MM-dd'.log'" />
<staticLogFileName value="false" />
<preserveLogFileNameExtension value="true" />
<appendToFile value="true" />
<countDirection value="1" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%d]: %15property{adrs}: %4t: %5p: %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
And when the IIS w3wp process starts, I get the expected filename of:
s_waq_2015-07-13.0.log
However, when IIS recycles the process, I get the filename (which is a bit a mouthful):
s_waq_2015-07-13.02015-07-13.log.0.log
This is occurring because IIS keeps the old process around, whilst the new process is starting. The new process can't open the expected filename because it is still in use. I don't want to use minimal locking because it is much slower (and this issue only occurs once per day).
Any ideas about how to get a more reasonable filename in this situation?
Many thanks
Ronny

A solution can by to include the processid in your logfile name:
<file type="log4net.Util.PatternString" value="C:\LogFiles\Staging_WebAQPro\s_waq_%processid_" />
This will fix your double date in the filename.

Related

Log4net error with XmlHierarchyConfigurator. Missing Filter Property

One of our WebApis stopped outputting logs recently. In the Trace file we were getting this message:
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [filter] to set object on [log4net.Repository.Hierarchy.Hierarchy]
The config file looks as below and I'm assuming the filter in the error is the same filter in the config so I'm not sure why it can't find it.
I upgraded to the latest version of log4net and that solved the issue for a few days and then we got the "Cannot find Property [filter]" error again. The config file hasn't changed at all over this period.
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</root>
<filter type="log4net.Filter.LevelMatchFilter">
<acceptOnMatch value="true" /> <!--change to false to exclude info logs -->
<levelToMatch value="INFO" />
</filter>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs/Log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1001KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level: %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
It's weird that it worked for a while, stopped, updgraded, worked for a while, stopped. I'm at at loss as to what could have changed to make it stop working either time.
Thanks in advance for any help.
Your configuration is not valid.
A filter can only be defined within an appender.
The documentation mentions:
Filters elements may only be defined as children of <appender>
elements.
Move the filter definition to the appender as shown below.
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<acceptOnMatch value="true" /> <!--change to false to exclude info logs -->
<levelToMatch value="INFO" />
</filter>
<file value="Logs/Log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1001KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level: %message%newline" />
</layout>
</appender>
(I can't explain why the error only occurs at certain/random times; probably it (re-)occurs at configuration time eg. after an website/appdomain recycle) ...
In the end, Log4net tries to keep continuing as much as possible.

log4net - specify which file to create. Do not create all appenders

I have a project in my solution that runs several different background jobs using Hangfire. I would like to be able to specify a different log file to be created using Log4Net based on the background job that is running.
For example;
job 1 does validation checks across a database and job 2 synchronizes data from a 3rd party app into another database
I have several appenders specified in the config file but all log files get created when I run job 1 or job 2. I want a log file only to be created per job e.g. when I run Job 1 log file job1.xml gets created and not job2.xml and vice versa
Is there any way of specifying the appender only? In the below example I want the file c:\logging\testing\jobs3-%date{ddMMMyyyy-hhmmss}.xml to be created only not the others
XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger("xmlFile2");
=====config file========
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="c:\logging\job1.log" />
<appendToFile value="true" />
<rollingStyle value="Once" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date - [%M] %level %logger - %message%newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="xmlFile" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="c:\logging\testing\job2-%date{ddMMMyyyy-hhmmss}.xml" />
<appendToFile value="true" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true" />
</layout>
<param name="Encoding" value="utf-8" />
</appender>
<appender name="xmlFile2" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="c:\logging\testing\jobs3-%date{ddMMMyyyy-hhmmss}.xml" />
<appendToFile value="true" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true" />
</layout>
<param name="Encoding" value="utf-8" />
</appender>
</log4net>

Why is the date on the file not rolling?

I must be doing something stupid but I can't think of it (I believe the core of my problem is that the PatternString is not dynamic and it gets set once when the program starts). Here is my lognet.config file:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="F:\Logs\MonitorService_%date{yyyyMMdd}.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="3000MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c :: %m%n" />
</layout>
</appender>
It seems like the files are coming out in a screwed up manner as:
F:\Logs\MonitorService_20170212.log
F:\Logs\MonitorService_20170212.log20170613
F:\Logs\MonitorService_20170212.log20170614
I would like them to come out as follows when they roll from day to day:
F:\Logs\MonitorService_20170612.log
F:\Logs\MonitorService_20170613.log
F:\Logs\MonitorService_20170614.log
What am I doing wrong?
There's no need to put the date in the file element's value.
The datePattern element determines the suffix applied to a log file when a new log file is created.
<file type="log4net.Util.PatternString" value="F:\Logs\MonitorService" />
<datePattern value="_yyyyMMdd" />
Here, the current log file will be called 'MonitorService' and when it is rolled on, the file will be renamed 'MonitorService_20170622' and a new file called 'MonitorService' will be created to store new log messages.

How to log in Azure Webjob with Log4net?

I have a web project and a console project. I've published my web project on Azure (after having the console app attached as a Azure Webjob to the web project).
Now I want to use Log4net to log in files. It works for the web project but I can't get it to work for the Webjob...
Here is my log4net config :
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="./_logs/[%date{yyyy-MM-dd-HH}]_webjob.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="4096KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline %date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<logger name="myloggername">
<level value="ALL" />
<appender-ref ref="FileAppender" />
</logger>
Some guy said it would work with a absolute path but it doesn't :
<file type="log4net.Util.PatternString" value="D:/home/site/wwwroot/App_Data/Logs/AccountMaintenance/log4net_%date{yyyyMMdd}.log" />
Please try the following value instead:
<file type="log4net.Util.PatternString" value="d:\home\logfiles\mylogfile.log" />

How to append rolling index BEFORE file extension in log filename

I am using the RollingLogFileAppender and it works great and it's rolling over into new files. But it's adding the .1, .2, etc at the very end of the file. So I end up with .log.1, .log.2, etc. So every file technically has a new extension that explorer doesn't know, so I can't just double click on a file to open.
How can I get the rolling file appender to insert that index BEFORE the file extension?
What I want is
.1.log
.2.log
Bonus would be for the current file to always be .0.log, that way they always sort correctly in explorer.
EDIT: added my current config settings
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="App_Data\\Logs\\" />
<datePattern value="dd.MM.yyyy'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<logger name="File">
<level value="All" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
You just need to use the PreserveLogFileNameExtension property on the RollingFileAppender.
See the following questions:
Log4net appender filename issue
Log4net RollingFileAppender Size rollingStyle file extension

Resources