log4net RollingFileAppender compress old files - zip

Is there an opportunity in log4net/RollingFileAppender to compress the old BackupFiles per configuration? I have found this property in the log4php, but not in log4net:
<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="default" class="LoggerAppenderRollingFile">
<layout class="LoggerLayoutSimple" />
<param name="file" value="file.log" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="5" />
</appender>
<root>
<appender_ref ref="default" />
</root>
</configuration>
About 2 years ago the Answer for the same Question was - "not possible". I wonder, if it is possible now.
Thank you.

Related

Swapping from log4net to Serilog using Common.Logging

A solution already exists that uses log4net with Common.Logging,
we have decided we want to swap out log4net and swap in Serilog.
In App.config
--<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1215">
++<factoryAdapter type="Common.Logging.Serilog.SerilogFactoryAdapter, Common.Logging.Serilog">
Seems easy enough, but then as I continued looking through our existing App.config file I came across less obvious areas of transition.
--<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
++<section name="serilog" type="Serilog.Configuration.LoggerSettingsConfiguration, serilog"/><!--Not sure if equivalent-->
This change does not seem as obviously right and is really just a shot in the dark. In fact, I'm pretty sure it is wrong.
Then there is the actual config of the rolling file appender:
<log4net>
<appender name="LM" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LogFileName}\\Program\Logs\\specificProgram.log" />
<param name="RollingStyle" value="Size" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="5" />
<param name="MaximumFileSize" value="10MB" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{MM-dd-yy HH:mm:ss.fff} [%thread] %-5p - %c{1} - %m%n" />
</layout>
</appender>
<appender name="QuickLog" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{LogFileName}\\Program\Logs\\QuickLog.log" />
<param name="RollingStyle" value="Size" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="5" />
<param name="MaximumFileSize" value="10MB" />
<param name="StaticLogFileName" value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{MM-dd-yy HH:mm:ss.fff} [%thread] %-5p - %c{1} - %m%n" />
</layout>
</appender>
<logger name="LM">
<level value="TRACE"/>
<appender-ref ref="LM"/>
</logger>
<logger name="QuickLog">
<level value="TRACE"/>
<appender-ref ref="QuickLog"/>
</logger>
</log4net>
I couldn't find any blogs or documentation on swapping from log4net to Serilog that use mainly App.config for settings...
In the code behind, we have a pretty simple initialization
if (null == s_logger || null == quick_logger)
{
log4net.GlobalContext.Properties["LogFileName"] = MachineConfiguration.DataPath;
s_logger = CommonLogging.LogManager.GetLogger("LM");
quick_logger = CommonLogging.LogManager.GetLogger("QuickLog");
}
Serilog also seems to lack a GlobalContext, or at least I haven't found the equivalent yet looking through the library.
Is there any mapping of App.config changes from log4net to Serilog? or somewhere to look up what the equivalent types or params are between the two libraries?
I know I can't be the first person to do this swap, but I am having trouble finding documentation that matches what we currently have in place.

Log4Net not logging error in release mode (.net console APP)

Hello i am using Log4Net in Console APP
Here is configuration in Assembly File
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Below is log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="InfoRollingLogFileAppender"
type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="WarningLogger.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="10240KB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<param name="Threshold" value="DEBUG"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%p [%d{dd MMM HH:mm:ss}][%l]- %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="WARN" />
</filter>
</appender>
<root>
<level value="debug" />
<appender-ref ref="InfoRollingLogFileAppender" />
<appender-ref ref="ErrorRollingLogFileAppender" />
</root>
</log4net>
</configuration>
For Initializing log4net i am using following configuration.
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
This things perfectly work in Debug mode. Same configuration is not working in release mode.
Let me know if any solutions
When you static method initializes the is no GetCurrentMethod().DeclaringType. That is probably why it does not work in release mode.
Got it resolved by adding up configuration on start up file or Program file in console .
XmlConfigurator.Configure(new FileInfo(ConfigurationManager.AppSettings["log4net-config-file"]));

where to put log4j.xml in netbeans 6.8?

In my case,
I have configured my log4j.xml like this.
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" />
<param name="File" value="F:/myLogger.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="20000KB" />
<param name="MaxBackupIndex" value="400" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %d{dd MMM yyyy HH:mm:ss.SSS} %-5l - %m%n%n" />
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value="error" />
<appender-ref ref="FILE" />
</root>
and put log4.xml file in source package.
but logger file is not created at specified folder.Let me know the exact path.
(it may be tha t i am using some external JAR which may have log4j.xml) so how to give priority to root application log4j.xml file.

Write hebrew with log4net

I write mixed Hebrew and English characters to a file using log4net but instead of Hebrew i see question marks.
I've tried setting Application's and Thread's culture info to 'he-IL' but it doesn't seem to help.
Does log4net support Hebrew characters? If so, how can i make it work?
Thanks
Edit:
This is my config section:
<appender name="ErrorsFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="c:\\taskman\\service.log"/>
<param name="AppendToFile" value="true" />
<rollingStyle value="Date"/>
<datePattern value="'service.'yyyy-MM-dd'.log'"/>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d;%m%n"/>
</layout>
</appender>
The hebrew text comes from the DB which is an SQL server 2005.
Add the following line to your app.config file, in the appender section:
<encoding value="windows-1255" />
After I tried adding the windows-1255 to the encoding value I still had issue with Hebrew text.
I ended up adding :
<encoding value="utf-8" />
And it worked only in UTF-8 mode
Encountered the same issue here.
I have configured log4net with the following settings:
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="giscraweler.txt" />
<encoding value="windows-1255" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level - %message%newline" />
</layout>
</appender>
</log4net>
I saw the font good in the Visual studio debug window, But when i opened the log file with Notepad++ - The text was not right, i saw weird characters.
How to fix ? Set up your "character sets" as first comment suggested.
I selected character sets windows-1255 and now i see the hebrew fonts.

log4net 1.2 RollingFileAppender not working

I'm using log4net v1.2 with a Windows Service App. My RollingFileAppender seems not to work. I'm pasting the logging sections of my service.exe.config below. Can anyone advise where I'm going wrong?
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
.....(lots of other config stuff)
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="D:\\Trinity\\Booking\\OneDay_PostTrade\\logs\\Trinity.log" />
<param name="MaximumFileSize" value="20MB" />
<param name="MaxSizeRollBackups" value="10" />
<param name="StaticLogFileName" value="true" />
<param name="Threshold" value="ALL" />
<param name="RollingStyle" value="Composite" />
<param name="appendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
...(stuff in between)
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
.....(stuff in between)
<logger name="CSFB.PostTradeRulesEngine">
<level value="ALL"/>
</logger>
The user your windows service is running as might not have write permission for the log file.
Another possibility is that you forgot to execute XmlConfigurator.Configure();
try writing:
<log4net debug="true">
it will post all errors to console.
thanks to everyone who responded. I dont know what i changed but my logging has started working fine.
Posting my logging sections. I didnt change anything in the code, except a line in the AssemblyInfo.cs:
[assembly: log4net.Config.Domain(UseDefaultDomain=true)]
Thanks again.:)

Resources