Empty logs a C# Rest WebService - c#

I Have A C# Rest webService which working fine.
I needed to add logs using the log4net .
It's working in DEV/Staging environnements but not in production.
I Have granted the Application pool & all the user of machine the required rights on my logs folder but still cannot see the logs.
I'am using 4.5.2 framework ,Windows 2012 R2 as a hosting environement
My web.conf
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="F:\logs\WebService\ManageParty\%property{LogName}_%date{yyyy-MM-dd}.log" />
<rollingStyle value="Date" />
<datePattern value="yyyy-MM-dd" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger{1} %property{LogName} - %message%newline" />
</layout>
</appender>
<logger name="*****.WS.ManageParty" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>

Related

log4net configuration for each project C#

I have 3 Class Lib projects as (ClassLib1.dll, ClassLib2.dll, ClassLib3.dll).
One Windows service project. WindowsService1.
Log for net is configure for Windows Service project and log4net is in config file as
<log4net>
<root>
<level value="all" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\{App_Name}\\{App_Name_Log_File_}" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>
</log4net>
Windows service is referring calling 3 Class Lib projects.
How can I configure log4 net for these 3 class lib so that I can have a log file in the separate folder as below?
For Main windows service
C:\XX\Logs\Win_Servive\Win_Service_Log_File_.log
For Class Lib 1
C:\XX\Logs\ClassLib1\ClassLib1_Log_File_.log
For Class Lib 2
C:\XX\Logs\ClassLib2\ClassLib2_Log_File_.log
For Class Lib 3
C:\XX\Logs\ClassLib3\ClassLib3_Log_File_.log
You can achieve this by configuring three different appenders on the web.config file, then loading three different loggers by name:
<appender name="Class1Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>
<appender name="Class2Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>
<appender name="Class3Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>
Then use the appropriate logger:
ILog class1Log = LogManager.GetLogger("Class1Appender");
ILog class2Log = LogManager.GetLogger("Class2Appender");
ILog class3Log = LogManager.GetLogger("Class3Appender");
Final log4net config as below
<log4net>
<root>
<level value="all" />
</root>
<logger name="WinService">
<appender-ref ref="WinService" />
</logger>
<logger name="ClassLibrary1">
<appender-ref ref="ClassLib1" />
</logger>
<logger name="ClassLibrary2">
<appender-ref ref="ClassLib2" />
</logger>
<logger name="ClassLibrary3">
<appender-ref ref="ClassLib3" />
</logger>
<appender name="WinService" type="log4net.Appender.RollingFileAppender" additivity="false">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\WinService\\WinService_" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>
<appender name="ClassLib1" type="log4net.Appender.RollingFileAppender" additivity="false">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\ClassLib1\\ClassLib1_" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>
<appender name="ClassLib2" type="log4net.Appender.RollingFileAppender" additivity="false">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\ClassLib2\\ClassLib2_" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>
<appender name="ClassLib3" type="log4net.Appender.RollingFileAppender" additivity="false">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\ClassLib3\\ClassLib3_" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>

Log4net logs are not getting updated on Windows Server 2012 until service restart

I am using log4net for logging in my windows service, it is working fine on XP, 7, 8, Windows Server 2003.
But when it comes to Windows 2012 server (also contains SQL server 2012 in it) log4net is not logging/updating the logs.
Log file is not updating after performing some operations in windows service. It needs restart of windows service to update the logs. Last/recent logs can be seen only after service restart.
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Log\MyLog_" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd.lo\g" />
<maxSizeRollBackups value="6" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<!--<conversionPattern value="%date %level %logger - %message%newline%exception"/>-->
<param name="ConversionPattern" value="%date %-5level %-60logger - %-10message%newline%exception" />
</layout>
</appender>
<appender name="SnFApp" type="log4net.Appender.RollingFileAppender">
<file value="Log\MyLog1_" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd.lo\g" />
<maxSizeRollBackups value="6" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<!--<conversionPattern value="%date %level %logger - %message%newline%exception"/>-->
<param name="ConversionPattern" value="%date %-5level %-60logger - %-10message%newline%exception" />
</layout>
</appender>
<logger name="TransactionManager.SaFManager.ClCCTaMPOSSaFManager">
<appender-ref ref="SnFApp" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
<logger name="Log4NetTest.OtherClass">
<level value="DEBUG" />
<appender-ref ref="ConsoleAppender" />
</logger>
As far as I'm concerned, it locks file exclusively by default. Could you try if this helps?
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
We've got it running on 2012 Server, too, without this issue.
It seems that you see the following behavior on Windows 2012 server, the logging is working, only the file timestamp is not updated:
file-date-modified-property-are-not-updating-while-modifying-a-file-without-closing-it
These days, we are monitoring this issue: when one was developing a
utility that monitors log files as they are updated.
On 2003, opening the log file folder in explorer, you can see the
timestamp and files size change before your eyes each time the log is
updated.
On 2008, "Last Modified" field on log files is not updated unless
another program attempts to open the file or the utility is stopped,
even if F5 is pressed to refresh the view.
Explorer gets is information from NTFS, by using a cmd prompt and
"dir" we found that the NTFS metadata for the files is not updated
until the handle to a file is closed.

Load only one appender instead of all

I'm currently working on a console C# app and I have to log two type of data :
All steps of the app in general (AppLog)
All information that I get from a service (MachineLog)
MachineLog needs 2 propeties, but when I start the app I do not define them I have some work to do before. But I need to log from start whith AppLog so at le beginning I use :
log4net.Config.XmlConfigurator.Configure();
And it create me the C:\DNC\Suivi\Logs.txt but also C:\DNC\Suivi\(null)\(null)-logs.txt
Is there any way to only load/configure AppLog and later MachineLog ?
I tried to add parameters to Configure() and all failed.
My App.config is :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="AppLog" />
</root>
<logger name="Machine" additivity="false">
<level value="ALL" />
<appender-ref ref="MachineLog" />
</logger>
<appender name="AppLog" type="log4net.Appender.RollingFileAppender">
<file value="C:\DNC\Suivi\Logs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="MachineLog" type="log4net.Appender.RollingFileAppender" >
<file type="log4net.Util.PatternString" value="C:\DNC\Suivi\%property{Machine}\%property{ID}-logs.txt" />
<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 %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
With your current configuration, you can't do it. As you have seen, log4net will create the log files when XmlConfigurator.Configure() is called.
In order to this, you will have to do at least some work programmatically, once you know where the file will be - it's quite easy to set the file name at runtime so you can have a placeholder file name in the config, then set the correct name at runtime when the values are known. This does mean that the empty placeholder file will exist - the only way around that would be to create the whole appender at runtime.

Log4net generating wrong log file name

I want to create log files names with the following pattern:
SBRF_20120820.log
SBRF_20120821.log
SBRF_20120822.log
SBRF_20120823.log
In other words, create a new file for each day. So, I create the following configuration to do that:
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<file type="log4net.Util.PatternString" value="Logs/SBRF_%date{yyyyMMdd}.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline" />
</layout>
</appender>
<logger name="LogEmArquivo">
<level value="INFO" />
<appender-ref ref="FileAppender" />
</logger>
</log4net>
When I run the program, today for example, the file SBRF_20120823.log will be created. But in the following days the log keeps to write in the SBRF_20120823.log file, and the files that are created are:
SBRF_20120823.log.2012-08-23
SBRF_20120823.log.2012-08-24
SBRF_20120823.log.2012-08-25
SBRF_20120823.log.2012-08-26
And if I run the program tomorrow, the files that will be created are:
SBRF_20120824.log.2012-08-24
SBRF_20120824.log.2012-08-25
SBRF_20120824.log.2012-08-26
SBRF_20120824.log.2012-08-27
Why?
You do not put the date pattern in the <file> - that is the static part of the filename. You need to put it into the <datePattern>.
Also, if you are using log4net 1.2.11, you can use <preserveLogFileNameExtension> which puts the datePattern on the current file also.
I think this is what you want your configuration to look like:
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<file type="log4net.Util.PatternString" value="Logs/SBRF_.log"/>
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<preserveLogFileNameExtension value="true"/>
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline" />
</layout>
</appender>
<logger name="LogEmArquivo">
<level value="INFO" />
<appender-ref ref="FileAppender" />
</logger>
Remove the <rollingStyle value="Date" />.
Don't think you change this behaviour with the RollingFileAppender, so you would have to create your own appender.

How to read and display my log file info in application using Log4net

I am using Log4net in my application.
Can anyone please explain how to read my log files(Txt) and show it in my application.because my log files are there in another server.
What will be the config settings to read the log files from the server and show it in the C# application.
This is the code have used to write the logs.
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="\\10.8.8.87\temp\logs.log" />
<appendToFile value="true" />
<maximumFileSize value="10MB" />
<maxSizeRollBackups value="50" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%identity---- %date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
Instead of an rollingfile appender you can better use for example a sql appender to log your exceptions to a database and from your application you can query the database. On this page you can find some config examples:
http://logging.apache.org/log4net/release/config-examples.html

Categories