log4net configuration for each project C# - 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>

Related

Empty logs a C# Rest WebService

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>

Log4net not writing logs to specified path

I am working with log for net i have below LOg4Net.config file code.But It is not writing logs to defined path .Am i doing anything wrong.Shall i miss anything??
Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net debug="true">
<appender name="InfoRollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Temp\Logs\FileTransferService\Info.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2MB" />
<staticLogFileName value="true" />
<!--<threshold value="INFO" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"></levelMin>
<levelMax value="WARN"></levelMax>
</filter>
<filter class="log4net.Filter.DenyAllFilter"/>
</appender>
<appender name="ErrorRollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Temp\Logs\FileTransferService\Error.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2MB" />
<staticLogFileName value="true" />
<!--<threshold value="ERROR" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR"></levelMin>
<levelMax value="FATAL"></levelMax>
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="InfoRollingLogFileAppender" />
<appender-ref ref="ErrorRollingLogFileAppender" />
<appender-ref ref="trace" />
</root>
<!--<logger name="CustomLogger">
<level value="ALL" />
<appender-ref ref="InfoRollingLogFileAppender" />
<appender-ref ref="ErrorRollingLogFileAppender" />
<appender-ref ref="trace" />
</logger>-->
</log4net>
</configuration>
Assembly Info Code:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
Output Directory :
Copy always

Log4Net Class Logger level per Appender-ref

I am trying to separate out a Performance Logger into a separate log using Log4Net...
I setup my new Appender
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\ServiceLayer.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="_yyyy.MM.dd.lo\g" />
<maximumFileSize value="5MB" />
<maxSizeRollBackups value="10" />
<PreserveLogFileNameExtension value="true" />
<layout type="log4net.Layout.PatternLayout">
<!-- <conversionPattern value="%level %thread %logger - %message%newline" /> -->
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %5level [%thread][%property{bms.log.username}::%property{bms.log.businessRequest}(%property{bms.log.activityLogID})] - %message%newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="PerfFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\Perf.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="_yyyy.MM.dd-HH-mm.lo\g" />
<maximumFileSize value="5MB" />
<maxSizeRollBackups value="24" />
<PreserveLogFileNameExtension value="false" />
<layout type="log4net.Layout.PatternLayout">
<!-- <conversionPattern value="%level %thread %logger - %message%newline" /> -->
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %5level [%thread][%property{bms.log.username}::%property{bms.log.businessRequest}(%property{bms.log.activityLogID})] - %message%newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
The perf appender is the new appender I added...
I also set up a BufferForwardingAppender
<appender name="BMSDefaultAppender" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="128" />
<lossy value="false" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN" />
</evaluator>
<appender-ref ref="RollingFile" />
From there I set my Root
<root>
<level value="ERROR" />
<appender-ref ref="BMSDefaultAppender" />
</root>
Once I had this setup, I get ERRORs logged to my rolling log file, however I want to capture some info level data into my perf file...
<logger name="BMS.LTD.Activity.Workflow" additivity="false">
<level value="INFO" />
<appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Activity.Workflow.Rules" additivity="false">
<level value="WARN" />
<appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Persistence.DataSourceDataProviders" additivity="false">
<level value="INFO" />
<appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Persistence.DataSourceDataProviders.DataSourceDataCache" additivity="false">
<level value="WARN" />
<appender-ref ref="PerfFile" />
</logger>
I now get my Info and Warn in my Perf log but I also get my errors there too, is it possible to get my ERRORs in my main log and my info levels in my perf logs?
To control what log events get into each file, Add LevelRangeFilters to your appenders:
<!-- log error and above - add to the RollingFile appender config -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR"/>
<levelMax value="FATAL"/>
</filter>
and
<!-- log INFO and below - add to the PerfFile appender config -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="INFO"/>
</filter>

I would like to create two log files - one with "DEBUG" another with "INFO" logs only

I have here this log4net config:
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\logfolder\logfile.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="20MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
</log4net>
This is working well, but I would like to create another logfile with only "INFO" input. Is it possible to log in two different files, without changing anything in my C# code, just changing the config file?
Or lets just make the question simple: What do I have to add exactly to my config file?
And also, I have another issue: if I choose "INFO" instead of "DEBUG", I get INFO + ERROR logs. can I change this somehow, not to get ERROR logs, just INFO? Thank you.
You can configure second appender with filter specified, and add it to your root logger.
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="AnotherLogFileAppender" />
</root>
<appender name="LogFileAppender"
type="log4net.Appender.RollingFileAppender">
...
</appender>
<appender name="AnotherLogFileAppender"
type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\logfolder\anotherlogfile.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="20MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern"
value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
</log4net>
Also consider to use new style of log4net configuration, rather than old one with param tags:
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="AnotherLogFileAppender" />
</root>
<appender name="LogFileAppender"
type="log4net.Appender.RollingFileAppender">
<file value="D:\logfolder\logfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="20MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
<appender name="AnotherLogFileAppender"
type="log4net.Appender.RollingFileAppender">
<file value="D:\logfolder\anotherlogfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="20MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO"/>
</filter>
</appender>
</log4net>

Log4Net Message log

I am trying to use log4net for my web service. the problem that I am facing is that, when I try to send multiple messages at the same time, it's only creating the log for the first message.
The log is working fine when we send only one message at a time and wait until the message has reached the listener. But when we shoot multiple messages at the same time (without waiting for the response at the listener), it's only logging the first message that I sent.
Here is my configuration. Can anyone please let me know what I am missing:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="ArchiveRollingFileAppender" />
<appender-ref ref="LogRollingFileAppender" />
</root>
<appender name="LogRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.PropertyFilter">
<key value="Source" />
<stringToMatch value="Log" />
</filter>
<threshold value="OFF"/>
<!--<lockingModel type="dndn.ebl.framework.logging.FileLock" />-->
<filter type="log4net.Filter.DenyAllFilter" />
<file type="log4net.Util.PatternString">
<conversionPattern value="{LogFileName}"/>
</file>
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%thread] %-5level [%property{method}] - %message%newline" />
</layout>
</appender>
<appender name="ArchiveRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.PropertyFilter">
<key value="Source" />
<stringToMatch value="Archive" />
</filter>
<threshold value="OFF"/>
<!--<lockingModel type="dndn.ebl.framework.logging.FileLock" />-->
<filter type="log4net.Filter.DenyAllFilter" />
<file type="log4net.Util.PatternString">
<conversionPattern value="{ArchiveFileName}"/>
</file>
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Did you try setting your lock model to
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

Categories