Log4Net composite RollingFileAppender with static file extension - log4net

Does the current version of Log4net have a way to create a RollingFileAppender with composite rolling style where the rolled files always preserves the given extension (.log in my case)?
Example of the format I would like:
MyLog.log
MyLog.2011-04-10.1.log
MyLog.2011-04-10.2.log
MyLog.2011-04-10.3.log
I found this post which says that there is a "PreserveLogFileNameExtension" property, but that it's not included in the official binaries. Is this still the case?
If so: Can anyone explain why this property is still not an offical part of Log4Net? I am a bit sceptical to use a custom build, but maybe I should not be?
I am also curious to know why the default functionality does not preserve the file extension. I do not see why it would gain the user that all the log files have different extensions.
Edit: Got it working by doing this:
1: Downloading and building the log4net source code
2: Applying these patches: https://issues.apache.org/jira/browse/LOG4NET-64
3: Setting PreserveLogFileNameExtension to "true" in the config.

Have you tried these parameters?
<file value="log-files\MyLog" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy-MM-dd'.log'" />
<param name="StaticLogFileName" value="false" />
It will preserve the extension, but will give you a date in every filename like this.
MyLog2011-05-16.log
MyLog2011-05-17.log
MyLog2011-05-18.log
MyLog2011-05-19.log
Maybe it is possible to combine this with the size rolling?

The situation is unchanged. There is no newer release of log4net. It is quite unclear to me when (if) there will be a new release...
I think you do not need to worry to much about using a custom build. Test your software, if it works it is good enough.
EDIT: There is a new release that should include LOG4NET-64. Of course you can still stick to your custom build.

I'm using this configuration:
<file value="" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
To get filenames like:
20111101.log
20111102.log
20111103.log

Related

Variable filename in FileAppender and RollingFileAppender in log4net

I am trying to use log4net to log to a file with a variable name using log4net.Util.PatternString.
The appender configuration looks like this:
<appender name="file2" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="c:\temp\MyLogFile_PID%processid.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level - %message%newline" />
</layout>
</appender>
With this configuration, the file is succesfully created with a name something like this:
MyLogFile_PID12345.log
I have succesfully used %processid, %random{8}, %env{SOME_ENV_VAR}, %property{MYPROPERTY} and %appsetting{someKey}.
But what I want is to insert a date, and no matter what I try, it seems that I cannot make it understand any of:
%date, %utcdate, %date{DATE}, %utcdate{DATE}, %date{ISO8601}, %date{ABSOLUTE}, %date{{HH:mm:ss}
or basically any form of date.
For example, what is wrong in:
<file type="log4net.Util.PatternString" value="c:\temp\MyLogFile_%date{ISO8601}.log" />
I have also tried to use a separate node for the conversion pattern configuration to no avail:
<file type="log4net.Util.PatternString">
<conversionPattern value="c:\temp\MyLogFile_%date{ISO8601}.log" />
</file>
Right now I am using a custom property (with %property{MY_CUSTOM_PROPERTY_WITH_THE_NAME_I_WANT}) to achieve a similar effect, but apart from being somewhat overkill, I wonder what I am doing wrong. I have tried in different computers and different applications and I cannot get what I intend.
By the way, my real aim is to use it in RollingFileAppender, but I am asking (and trying) here about FileAppender just for simplicity sake.
Any help?
I see you're using the type log4net.Util.PatternString directly instead of the appender log4net.Appender.FileAppender, any specific reason for that?
I'm just looking at the examples from https://logging.apache.org/log4net/release/config-examples.html
I finally got it!
It seems the only way it works is specifying a date format string, and with NO ':' in it.
Despite the examples in https://logging.apache.org/log4net/release/sdk/index.html, where it uses %date{HH:mm:ss,fff}, %date{dd MMM yyyy HH:mm:ss,fff}, %date{ISO8601}, %date{ABSOLUTE} none of them seem to work!
But this (and similar ones) finally do:
<file type="log4net.Util.PatternString" value="c:\temp\MyLogFile_%date{yyyy-MM-dd_HH-mm-ss}.log" />
I am still scratching my head if I was doing something wrong or the documentation is just plain confusing.
I think it has to do with the fact that the character ':' is allowed in Linux filenames, but not on Windows (I am using Windows, as 90+% of log4net users, I guess). And ISO8601 and ABSOLUTE don´t work either probably because in both cases log4net translates them to a string with ':' in it, failing for the same reasons.
If this is true, being a logger for .NET, a framework that is overwhelmingly used in Windows systems, I think this is something they should have in mind.
Problem solved (albeit with some doubts).

C# - log4net rollover on date, size, and application relaunch

I'm working on a wpf application that uses log4net. It currently logs to a single file and only rolls over when it grows too large. I am trying to modify this so that it grows over when the file grows too large, when the date changes, or when the application is relaunched.
I am trying to get output as close to the following as possible
App_2017-07-06.0.txt //First launch on 2017-07-06
App_2017-07-06.1.txt //Rollover due to size limit
App_2017-07-06.2.txt //Application relaunch
App_2017-07-06.3.txt //Rollover due to size limit
App_2017-07-07.0.txt //Rollover due to date change
App_2017-07-07.1.txt //Rollover due to size limit
App_2017-07-07.2.txt //Application relaunch - Currently Logging File
From what I understand from the documentation, The rolling style can be set to "compostite" to capture date and size or it can be set to "once" to capture application relaunches. It doesn't seem to have a way to do all 3: http://logging.apache.org/log4net/release/sdk/html/T_log4net_Appender_RollingFileAppender_RollingMode.htm
I got it to the point where everything looks correct and works except the application relaunch overwrites a pre existing file (I imagine because of the appendToFile property). I just cant seem to get it working the way I need to and I can't find any answers in the documentation.
This question seems to be trying to achieve a similar goal, but did not solve my problem:
How do I force a rollover at application startup with Log4net RolloverFileAppender?
Am I missing something? Is it just not possible to do this with log4net?
My current configuration
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Log\App.txt" />
<appendToFile value="false" />
<rollingStyle value="Composite" />
<maximumFileSize value="10KB" />
<maxSizeRollBackups value="-1" />
<staticLogFileName value="false" />
<preserveLogFileNameExtension value="true" />
<countDirection value="1" />
<lockingModel type="log4net.Appender.RollingFileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level User = %username Class = %property{ClassName} Method = %property{MethodName}%newlineMessage - %message%newline%exception%newline***************************************" />
</layout>
</appender>
My current output
App_2017-07-06.0.txt //First launch on 2017-07-06
App_2017-07-06.1.txt //Rollover due to size limit
App_2017-07-06.1.txt //Application relaunch - overwrites pre-existing file
App_2017-07-06.2.txt //Rollover due to size limit
App_2017-07-07.0.txt //Rollover due to date change
App_2017-07-07.1.txt //Rollover due to size limit
App_2017-07-07.1.txt //Application relaunch - overwrites pre-existing file - Currently logging file
As far as I know log4net copies the log file to xxxx.{number}.txt when it rolls over to the next file. So your first App_2017-07-06.1.txt was never created and is still called App_2017-07-06.txt when the application restarts. You have configured so App_2017-07-06.txt will be overridden and your first App_2017-07-06.1.txt was never created.
You can configure <appendToFile value="true" /> to not override the existing file.

Embed a video object in html

I have this assignement where I have to used export for web in quicktime pro 7. Then copy/paste the read me.html file into the body (exactly as it is), which I did.
However, I keep getting 3 errors when I run my .html through W3C and cannot figure out how to correct it.
I realized there are probably better way to embed object but this is how they want us to do it for this assignment. Can anyone help me correct these 3 errors please.
Many thanks in advance.
<object width="350" height="278">
<param name="src" value="swiss/swiss-poster.jpg" />
<param name="href" value="swiss/swiss.mov" />
<param name="target" value="myself" />
<param name="controller" value="false" />
<param name="autoplay" value="false" />
<param name="scale" value="aspect" />
<embed width="350" height="278" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"
src="swiss/swiss-poster.jpg"
href="swiss/swiss.mov"
target="myself"
controller="false"
autoplay="false"
scale="aspect">
</embed>
</object>
Line 1 : Element object is missing one or more of the following attributes: data, type.
Line 14: Attribute href not allowed on element embed at this point.
scale="aspect">
Line 15: Stray end tag embed.
I just needed to embed a quicktime movie myself, and I looked to the Mozilla Developer Network for the syntax: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object
I believe that the data attribute would take the place of href in your params:
param name="href" value="swiss/swiss.mov" />
would be:
<param name="data" value="swiss/swiss.mov" />
and the type in this case would be "video/quicktime":
<param name="type" value="video/quicktime" />
There is also an <embed> reference at MDN (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/embed), but it covers only the HTML5 version of the element, which doesn't require a closing </embed> tag.
The reference at sitepoint (http://reference.sitepoint.com/html/embed) addresses the earlier syntax, and also says:
"embed isn’t part of any currently recognized standard (it is included in HTML5 which is not yet finalized), so if you use it, your page can’t possibly validate".
That suggests that with <embed> you should probably focus more on actual behavior in the browser than the W3C's validation tool.

Log4Net RollingFileAppender generates duplicate logs

I have a WCF service running on a single server, using Log4net to track usage via INFO and WARN level log entries. Using a RollingFileAppender with the following very standard config:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="\\mylocation\data\PRD\myApp\MyService"/>
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="-yyyy-MM-dd'.log'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="ADONetAppender_SqlServer" />
</root>
I also use an ADONetAppender, which receives redirected "WARN" level data and writes it to a DB table in SQL server via a stored procedure. The config for this is a bit long, so I have omitted it for readability.
I have this setup in our Dev and TST environments where it has been running fine. In the PRD environment, it seems to generate duplicate log files. The first is named according to my specified pattern i.e. "logfile-yyyy-mm-dd.log". The second file looks like an addition to the first, with the date pattern duplicated i.e. "logfile-yyyy-mm-dd.log.-yyyy-mm-dd.log".
Making this more interesting is that entries contained in the two files overlap by time. File 1 might have entries from 8am to 12am, and file 2 will also contain entries from the same time period. The entries are not duplicates, they are generated by different users of the service. The copies of Files 1 and 2 can be pretty much any size, so this is not an issue of reaching a size or date/time threshold and generating the next required log file.
The DB table entries contain all the expected rows, some contained in each of the log files. These rows are generated only by WARN level logging, and some WARNings appear in each log file.
I have bounced this off some log4net savvy folks in our shop, but nobody has a good idea of what might be causing this duplicate file behavior. Any ideas from Stackland appreciated.
Your date pattern shouldn't have .log after it. I also am unsure why you have two appenders declared in the root. You should be able to get rid of the root altogether, given the rest of your config it has no purpose (assuming you don't have more that isn't in the example).
I've found that this happens when the file being logged to is locked by another thread or process.
I'm assuming Log4Net creates the other file because it can't log to the configured logfile and thus creates a new file and logs to it, but I'd have to go through the log4net code to be sure of that assumption.
Try adding
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
to the appender element to minimize the amount of locking. Just be aware that there is additional overhead associated with using MinimalLock: http://logging.apache.org/log4net/release/sdk/log4net.Appender.FileAppender.MinimalLock.html
I had the same exact problem.
I confirm that was a permission issue. In my case the logfiles were generated by two different accounts (from a scheduled task and sometimes by a manual run via console), and in this case the file name had a duplicate date pattern.
After resetting the permissions to both users (the scheduled process user and the interactive users) the issue did not repeat anymore.
Greets,
Michele

log4net + multiple threads + rolling file appender

I've got this settings for log4net in the log4net.config to allow multiple threads to write to the same file:
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<!-- Minimal locking to allow multiple threads to write to the same file -->
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="log\UI.log"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<maxSizeRollBackups value="30"/>
<datePattern value="-yyyyMMdd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level [%property{identity}] %logger{3} - %message%newline"/>
</layout>
</appender>
But after midnight the new created log file is being overwritten all the time and thus there is only the last one event in the file. After server restart it all goes right again till the next midnight.
So can anyone say whether this is a config issue or this is just a log4net issue?
The problem was solved by removing the locking model key since I have only one process (IIS, w3wp.exe) which uses the same logger.
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
Since it was said here:
If you use RollingFileAppender things become even worse as several process may try to start rolling the log file concurrently. RollingFileAppender completely ignores the locking model when rolling files, rolling files is simply not compatible with this scenario.
I think you will get unpredictable results.
My guess is that your use of a - sign on the datePattern is confusing the framework so that after the first roll any log triggers a roll event.
What happens when you try this with
<datePattern value="yyyyMMdd" />
per the example here.
To change the rolling period adjust the DatePattern value. For
example, a date pattern of "yyyyMMdd" will roll every day. See
System.Globalization.DateTimeFormatInfo for a list of available
patterns.

Resources