I would like to store log4net config data in my application.config file. Based on my understanding of the documentation, I did the following:
Add a reference to log4net.dll
Add the following line in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Initialize the logger as follows:
private static readonly ILog log = LogManager.GetLogger(typeof(frmWizard));
I have the following code in my app.config:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
However, when I run the application, I get the following error on the console:
No appender named [Consoleappender] could be found.
How can I get log4net to read settings from the config file?
Thanks!
You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.
log4net is a port of the popular Apache log4j™ logging library. The initial port was done in June 2001, since then we have tried to remain in the spirit of the original log4j. See the log4net history page for more details.
RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.
Add a line to your app.config in the configSections element
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" /> </configSections>
Then later add the log4Net section, but delegate to the actual log4Net config file elsewhere...
<log4net configSource="Config\Log4Net.config" />
In your application code, when you create the log, write
private static ILog GetLog(string logName) { ILog log = LogManager.GetLogger(logName); return log; }
From the config shown in the question there is but one appender configured and it is named "EventLogAppender". But in the config for root, the author references an appender named "ConsoleAppender", hence the error message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With