Log4Net Config setup error : No appender named [FileAppender] could be found - log4net

I have created a lognet library using .Net Core 6.0.
The library class has the following initialization.
public static class MyLogger
{
private static readonly ILog logger = LogManager.GetLogger("");
static MyLogger()
{
log4net.Config.XmlConfigurator.Configure();
}
}
In my console application's app.config has the following settings,
<?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 value="C:\\Logs\\Test.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="Info" />
<level value="Warn" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
I research didn't help me to resolve the issue. Thanks for your help.

Change your root config to use an existing appender:
<appender-ref ref="RollingFileAppender" />

Related

Back up Log file with different extension

I am using below config file for log generation
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
</configSections>
<log4net>
<appender name="TestLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="${ALLUSERSPROFILE}////Logs//Test.log" />
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="TestLogFileAppender"/>
</root>
<logger additivity="false" name="TestLogger">
<appender-ref ref="TestLogFileAppender"/>
</logger>
</log4net>
</configuration>
My expectation is to get the log file in below format
Current Log file name Test.log.
When it reaches to 5MB, expecting back up file name as below
Test_1.bak
:
"
:
:
Test_10.bak

log4Net filtering for multiple outputs confusion

Trying to understand the log4net filtering.
After reading the docs some aspects are still elusive to me.
For example I have this config file:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start" />
<footer value="Service Logging -- End" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="WindowsService1.exe" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
<!-- Specify the level for specific categories (“namespace.class”)-->
<logger name="WindowsService1.MyMonitor">
<level value="WARN" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
My expectations were:
The RollingFileAppender has a range of [INFO:FATAL] The Root has a
Level of DEBUG so it would let DEBUG and "greater" pass. Which
means all of RollingFileAppender's range would go through.
The LogFileAppender has no range specified so it would use Root's of DEBUG or greater.
And the EventLogAppender would only send event messages of WARN or greater to the Windows Event System - and - they would only
be messages from the class "WindowsService1.MyMonitor".
However it appears that all the messaging gets filtered to the WARN level!
Including my rolling log files and the log file "log-file.txt"
This seems like it should be simple - don't know why I'm so confused by it.
Hope someone can enlighten me.
Please feel free to use crayons and pictures because I think I need them.
Thanks.
UPDATE
In my Program.cs I have:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
And in each class where logging is done:
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
UPDATE 2
At a different computer now. Had to recreate everything and used log4net debugging to assist.
But still can't get the desired output.
Basically want the WARN and above messages to go to one file and the Event logger. And those messages would be a subset of all the INFO and above messages.
The INFO and above messages - superset - would go into the rolling appender and overwrite themselves eventually. But the warnings and errors would always be maintained.
So here is a rework of all the files for a simple service. If I set WARN on the appender-ref ref=EventLogAppender then all the INFOs are suppressed.
Program.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
// Ref: http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/
//
// use 'sc' to install/delete the service
// run 'sc' as administrator
// sc create "MySillyService" binpath= "C:\whatever\something.exe"
// sc delete "MySillyService"
namespace SimpleService1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
Service1.cs
namespace SimpleService1
{
public partial class Service1 : ServiceBase
{
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private Timer timer1;
private int counter = 0;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
timer1.Interval = 4000;
timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
timer1.Enabled = true;
log.Info("my service is started");
}
private void timer1_Tick(object sender, ElapsedEventArgs e)
{
counter++;
if (counter%2 == 0)
{
log.Info(String.Format("Counter({0:D}) is now even", counter));
}
else
{
log.Error(String.Format("Counter({0:D}) is now even", counter));
}
}
protected override void OnStop()
{
timer1.Enabled = false;
log.Info("my service was stopped");
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<!--
Ref: http://mitch-wheat.blogspot.com/2007/04/log4net-net-logging-tool.html
I keep this config file in a separate directory and copy it with a
post-build event:
copy /Y $(SolutionDir)Logger\Log4Net.config $(TargetDir)Log4Net.config
-->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start - " />
<footer value="Service Logging -- End - " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Service1" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
<!-- Specify the level for specific categories (�namespace.class�)-->
<logger name="SimpleService1.Service1">
<level value="WARN" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
Thanks for your help
Finally figured out the nuances to get this working as desired. Here's the config for anyone interested.
<?xml version="1.0" encoding="utf-8" ?>
<!--
I keep this config file in a separate directory and copy it with a
post-build event:
copy /Y $(SolutionDir)Logger\Log4Net.config $(TargetDir)Log4Net.config
-->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start - " />
<footer value="Service Logging -- End - " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<!-- Put whatever name you want to display in the event log
for 'applicationName value=XX'
-->
<applicationName value="ServiceSkippy" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- put namespace of service application in following name -->
<logger name="ServiceWithLog4Net">
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
-Skippy

Problems with Log4net multiple logger to multiple files

log4net version: 1.2.13.0
Runtime Version: v4.0.30319
I've got some Problems with using multiple Loggers and Appenders.
I have a root Logger with 3 Appenders which work well but when im configuring multiple other loggers log4net seems to only register the appenders from the last logger specified in the app.config.
If i put TaskSyncLogger as last logger entry TaskSyncAppender gets recognized
If i put CalendarSyncLogger as last logger entry CalendarSyncLogger gets recognized
only the 3 appenders from the root logger and the appender from the last logger entry is listed in hierarchy.GetAppenders()
Hierarchy hierarchy = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
var appenders = hierarchy.GetAppenders();
my app.config
<appender name="MainFileAppender" type="log4net.Appender.FileAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger- %message%newline %exception" />
</layout>
<file value="Logs\logs.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline %exception" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline %exception" />
</layout>
</appender>
<appender name="TaskSyncAppender" type="log4net.Appender.FileAppender">
<appendToFile value="true"/>
<file value="Logs\logsTask.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger- %message%newline %exception" />
</layout>
<!--
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
<filter type="log4net.Filter.DenyAllFilter" />
</filter>
-->
</appender>
<appender name="CalendarSyncAppender" type="log4net.Appender.FileAppender">
<appendToFile value="true"/>
<file value="Logs\logsCalendar.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger- %message%newline %exception" />
</layout>
<!--
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
<filter type="log4net.Filter.DenyAllFilter" />
</filter>
-->
</appender>
<root>
<level value="All" />
<appender-ref ref="MainFileAppender" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<logger Name="CalendarSyncLogger" additivity="true">
<level value="All"/>
<appender-ref ref="CalendarSyncAppender"/>
</logger>
<logger Name="TaskSyncLogger" additivity="true">
<level value="All"/>
<appender-ref ref="TaskSyncAppender"/>
</logger>
further the TaskSyncAppender isn't writing anything the File gets created but it remains empty although the the MainFile Appender is writing the output.
im getting it like
public const string TaskSyncLogger = "TaskSyncLogger";
private static readonly ILog logTask = LogManager.GetLogger(LoggingStrings.TaskSyncLogger);
I can reproduce this, and while I cannot be confident in saying it is definitely a bug, it is certainly unexpected.
You can work around it by adding this last logger to your file:
<logger Name="WillNotBeUsed">
<level value="Off"/>
<appender-ref ref="CalendarSyncAppender"/>
<appender-ref ref="TaskSyncAppender"/>
</logger>
After that all five appenders are loaded.

log4net write null to file unexpectedly

Does anybody encounter the error from log4net that many null are written with RollingFileAppender unexpectedly when the system gets hang?
The file log will be like below:
2014-06-05 11:40:45,993 [5156] INFO myapp.test.Main getStatus
2014-06-05 11:40:45,995 [5156] INFO myapp.test.Main getStatus
[NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL][NUL]2014-06-05 11:51:58,418 [2992] INFO myapp.test.Main Start...
log4net Version: 1.2.10
Below is log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="C:\Test\Log\log4net-log.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Test" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>

log4net ForwardingAppender threshold

I need to configure logger to write messages to several appenders with different Log Levels.
For ex. I want RefreshDataService to log detailed info to TransportFileAppender and Warnings and Errors to RollingFileAppender.
Also, I'm using buffering to improve logging performance.
I setup FilteredFileLog appender to forward Warnings to FileLog->RollingFileAppender.
But for RefreshDataService logger, all log messages including debug and Info getting logged to FileLog.
Can you help me to configure the RefreshDataService logger to log only Warn+ messages to FileLog? Thanks.
Below is excerpt from my log4net config.
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="${TEMP}\logs\client\${ENV}\client.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="4096KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level [%property{ENV}] [%property{UID}] %logger - %message%newline" />
</layout>
</appender>
<appender name="TransportAppender" type="log4net.Appender.RollingFileAppender">
<file value="${TEMP}\logs\client\${ENV}\client.comm.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="4096KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="DetailedTransportLog" type="log4net.Appender.BufferingForwardingAppender">
<threshold value="DEBUG" />
<bufferSize value="200" />
<appender-ref ref="TransportAppender" />
</appender>
<appender name="FileLog" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="20" />
<lossy value="false" />
<appender-ref ref="RollingFileAppender" />
</appender>
<appender name="FilteredFileLog" type="log4net.Appender.ForwardingAppender">
<threshold value="WARN" />
<appender-ref ref="FileLog" />
</appender>
<logger name="RefreshDataService" >
<level value="DEBUG" />
<appender-ref ref="FilteredFileLog" />
<appender-ref ref="DetailedTransportLog" />
</logger>
You can use a level range filter on the appender:
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
My understanding is that the Threshold property is used tell the appender to flush the buffer when messages of a certain level are logged. This way you can make for instance sure that errors are written immediately to the database.

Resources