log4net:ERROR Failed to find configuration section 'log4net' - c#

I get this error when I try to configure my logger using:
log4net.Config.XmlConfigurator.Configure();
The app.config looks like this:
<?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="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\Logs\my-log-file.txt" />
<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 %logger [%C{1}.%M] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
I'm trying to write separate logs for each component of a system. I have one working for the main class but I get this error when trying to configure another one for a different class. What is the cause?
Thanks in adv!

I ended up using the following to configure my Add-In and RTD:
[Add-In]
log4net.Config.XmlConfigurator.Configure(
new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "MyAdd-In.dll.config"));
[RTD]
log4net.Config.XmlConfigurator.Configure(
new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\RtdServer.dll.config"));
Which then configured the separate logger for my RTD.
Hth

Related

How to use log4net in a DLL project?

I have a project in which I want to use log4net for logging. When I'm testing my project using a console app program, it does not log anything unless I write the log4net section in the app.config of the test project. How do i fix this ?
This is my configuration in my app.config file in the DLL:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MyLogFile.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value ="DEBUG"/>
<appender-ref ref ="RollingFileAppender"/>
</root>
</log4net>
From log4net documentation page: https://logging.apache.org/log4net/release/manual/configuration.html (Configuration Files section)
The only way to configure an application using the System.Configuration APIs is to call the log4net.Config.XmlConfigurator.Configure() method or the log4net.Config.XmlConfigurator.Configure(ILoggerRepository) method.
Make sure you are initializing log4net on your console app.

Quartz.Net Jobs to Log File

I'm currently creating a scheduler using Quartz.net with the CronSchedule. I'm wanting to send information regarding the 'Jobs' such as time executed and finished to a .txt file or something which logs the information, however I don't know how to approach this.
Any help?
You can use log4net, I suggest to log separate for each Job.
Before to start scheduler:
var location = System.Reflection.Assembly.GetExecutingAssembly().Location;
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(location + ".config"));
In each class that extends IJob (so, in each Job):
var logger = log4net.LogManager.GetLogger("TestJobLogger");
Where obviously "TestJobLogger" is different for each Job.
In the App.config insert this, one <logger> and one <appender> for each Job you have:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
...
<log4net>
<appender name="AppenderTestJob" type="log4net.Appender.RollingFileAppender">
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value=".yyyyMMdd-HH" />
<param name="ImmediateFlush" value="true" />
<maxSizeRollBackups value="10" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="C:\PathToSaveFile\" />
<datePattern value="'test.'yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd-MM-yyyy HH:mm:ss.fff} [%t] [%-5p] (%c) - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
</root>
<logger name="TestJobLogger" additivity="false">
<level value="INFO" />
<appender-ref ref="AppenderTestJob" />
</logger>
</log4net>
Now, you can log simply with logger.Debug("TESTTTTTT");.

log4net log file is not getting created in C#

log file is not getting created,
using below call to get logger and config file.
is it required to give full path for file?
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" requirePermission="false"/>
</configSections>
<!-- Log4net Logging Setup -->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="50" />
<maximumFileSize value="50MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
Does your code call
log4net.Config.XmlConfigurator.Configure();
on startup?
The <root> section I use has priority, not level:
<root>
<priority value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
(Apparently, that's not the issue https://stackoverflow.com/a/24188507/21336)
after adding below line in AssemblyInfo.cs, could see log file getting created and logs coming.
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

log4net not logging errors and fatal in separate file

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.

Cancel console printing

I have the following config file:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="D:\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="20" />
<maximumFileSize value="4MB" />
<staticLogFileName value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
Every line I am logging is written to Console as well, how can I cancel it?
I want it to be appended to the log.txt file ONLY, without anything in Console.
Either you have added console appender programmatically, or this configuration file is not used by log4net. Log4net don't add any default appenders (like default TraceListener in .net). And this configuration specifies only RollingFileAppender. So, verify which configuration file is used, and whether some appenders added in the code.

Categories