Missing date and extension with log4net - log4net

I've just downloaded log4net via NuGet in VS2010. Log4net version 1.2.11.0.
My issue - I can't append the date or extension to the file name.
Here's my appender:
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Error" />
<appendToFile value="true" />
<datePattern value=".yyyy-MM-dd.lo\g" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
The file is created in the correct directory (./Logs) with name "Error", no extension.
I've also tried datePattern ".yyyy-MM-dd.\l\o\g".
None of the other S.O. questions I've visited have helped. Any ideas?

Add the line
<staticLogFileName value="false" />
For details see
http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.StaticLogFileName.html

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.

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" />

log4net generated file has a wrong (null) name

I'm using log4net to log things. In my web application, the Web.config file contains the following section:
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="\\PathWhereThingsAreLogged\%property{HostName}-NameOfTheApplication" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<maximumFileSize value="10MB"/>
<maxSizeRollBackups value="5" />
<rollingStyle value="Size" />
<rollingMode value="Size" />
<datePattern value=".yyyy-MM-dd'.log'" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (%file:%line) %-5level - %message%newline" />
</layout>
</appender>
Logs appear in the file, so that's cool, but sadly the name of the file is incorrect. Its name is "(null)-NameOfTheApplication" instead of using the %property{HostName} declared in the file declaration.
Any help would be appreciated, thanks!
Actually, properties must be declared beforehand!
In a web application, put that in your Global.asax.cs file, in the Application_Start() method:
GlobalContext.Properties["HostName"] = Environment.MachineName;
In a windows service or a job, put it in the Program.cs in the Main(string[] args) method.

Is there a way to have log4net create log file only when there is logging activity?

We changed our log4net file name to include the current date but only want log files created on those dates that there is logged activity. This is our current configuration:
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="{0}\\logfiles\\log%date{{yyyyMMdd}}-${{COMPUTERNAME}}.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger %message%newline" />
</layout>
</appender>
<root>
<level value="WARN" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
It's producing empty files on days when the logger is created but no messages of WARN or greater are written. Is there a configuration to prevent empty files?

Resources