Write hebrew with log4net - 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.

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.

Why log4net log message have some unknown character?

I use log4net for logging my logs.
and use configuration here (only log the message):
<logger name="LogTracking">
<level value="INFO" />
<appender-ref ref="LogTrackingAppender" />
</logger>
<appender name="LogTrackingAppender" type="log4net.Appender.RollingFileAppender">
<file value="logging\urltracking\" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="20MB" />
<encoding value="utf-8" />
<datePattern value="yyyy'\\'MM'\\'dd'\\'yyyyMMddHH'.log'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
But when i open my logs file, if in UTF-8 encoding the lines message seems normal. And if in ANSI encoding, these lines message have a unknown character at the beginning of the lines.
See here:
<?xml version="1.0" encoding="utf-8"?><UrlTrackingObj><Url>mysite</Url><Action>view</Action><Ip>::1</Ip><Os>Windows 8.1</Os><Browser>Chrome 59.0</Browser></UrlTrackingObj>
Then when my app reads log lines, it can read and process this character.
What is my configuration wrong? or any solution this solve this problem?
I want my log line only:
<?xml version="1.0" encoding="utf-8"?><UrlTrackingObj><Url>mysite</Url><Action>view</Action><Ip>::1</Ip><Os>Windows 8.1</Os><Browser>Chrome 59.0</Browser></UrlTrackingObj>
Those characters  are the byte order mark (BOM):
The UTF-8 representation of the BOM is the (hexadecimal) byte sequence
0xEF,0xBB,0xBF. A text editor or web browser misinterpreting the text
as ISO-8859-1 or CP1252 will display the characters  for this.
You can use the other encoding class rather than the default one to suppress the BOM such as:
<encoding type="System.Text.UTF8Encoding"/>
System.Text.UTF8Encoding

why files not delete for maxDateRollBackups settings

I have below log4Net configuration,
<appender name="WhateverYouNameThis" type="log4net.Appender.RollingFileAppender">
<threshold value="All" />
<file value="logs\WhateverYouNameThisFile.log" />
<appendToFile value="true" />
<maxDateRollBackups value="2" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="2KB" />
<rollingStyle value="Composite" />
<staticLogFileName value="true" />
<datePattern value="yyyyMMdd-HH.lo\g" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
"maxSizeRollBackups =2" works fine. When file gets larger than 2KB, it will roll to another file, up to 2 times, and then these will start getting deleted too if it goes over 2 files.
but "maxDateRollBackups=2" is not working. Any files older than 2 days is not deleted. Please suggest the reason!
When date change the file rename with 1 day before date, but files older than 2 days is not delete,
This issue seems known in log4net. These issues https://issues.apache.org/jira/browse/LOG4NET-27 / https://issues.apache.org/jira/browse/LOG4NET-367 describes the major problems with the rolling file appender. If you have any new information, I am interested in solutions as well.

How to change the log4net rolling filenames to log_YYYMMDD_HHmmss.txt

I am maintaining some c# code and I want to log4net to store old log files as:
log_YYYMMDD_HHmmss.txt
eg:
log_20140617_193526.txt
I believe this is the relevant part of the config file, with my attempts at modifying it...
<appender name="HourlyAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString"
value="${ALLUSERSPROFILE}/Optex/RedwallServer/Log/log.txt" />
<appendToFile value="false" />
<datePattern value="yyyyMMdd_HHmmss.\tx\t" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="" />
<param name="Footer" value="" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
It is producing a current log file of:
log.txt
And old log files are stored like:
log.txt20140617_193526.txt
Anyone any idea how I can change the prefix from "log.txt" to "log_"?
What I would really like is to figure this out myself, but I can't for the life of me find any decent documentation. I found this on rollingConfig but it is not what I'm after...
http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.html
It seem you have to change log.txt to log_:
<file type="log4net.Util.PatternString"
value="${ALLUSERSPROFILE}/Optex/RedwallServer/Log/log_" />

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