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
Related
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>
I tried to separate the errors and fatal into separate log file not i dont see it logging in separate file. here is my config file. can you point what mistake i made?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\NinjaTraderLogs\NinjaLogs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level (%property{method}) (%property{state}) (%property{instrument}) - %message%newline" />
</layout>
<level value="All" />
</appender>
<appender name="FatalError" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\NinjaTraderLogs\NinjaLogs_Fatal.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level (%property{method}) (%property{state}) (%property{instrument}) - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Create one more appender ref as below under <root>
<root>
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="FatalError" />
</root>
Before you call your logging in code make sure to call the XmlConfigurator function Configure (call it globally) like
log4net.Config.XmlConfigurator.Configure();
Last but not least, make sure you have write permission to the directory you are trying to create log file. I would suggest you to check windows event log once Application log to see if any error has happened.
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>
my AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
i change the structure just like you said
(i just change the <file value="..\Logs\\CurrentLog" /> to <file value="D:\Log\Log.txt" />):
<?xml version="1.0" encoding="UTF-8"?>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\Log\Log.txt" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
and define the log:
private static readonly ILog Log = LogManager.GetLogger("Logger");
but still the log.txt file doesn't created yet!
There's something missing in your config file.
<log4net>
<logger name="default">
<level value="ALL"/>
</logger>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\\CurrentLog" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
And also the declaration:
private static readonly ILog Log = LogManager.GetLogger("default");
Actually, you haven't declared a logger in your config and it has wrong structure.
This is how it should look like:
<?xml version="1.0" encoding="UTF-8"?>
<log4net>
<logger name="Logger">
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</logger>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\\CurrentLog" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
Now you can access it this way:
private static readonly ILog Log = LogManager.GetLogger("Logger");
It definitely works.
Now about your code snippet. I'm not sure about your initialization. I haven't worked with log4net this way:
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
However, you can try declaring logger in root and initialize it:
<?xml version="1.0" encoding="UTF-8"?>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\\CurrentLog" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
// ...
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
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" />