Configuring log4net with xml file
Solution 1
When you have log4net config in a separate config file you should not include the configuration and configSections elements. log4net should be the top level element after the xml declartion.
Solution 2
Try this custom configuration through code. Worked for me and tested in multiple apps..
string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "\\Config\\Log4Net.config");
FileInfo finfo = new FileInfo(logFilePath);
log4net.Config.XmlConfigurator.ConfigureAndWatch(finfo);
I understand it's a very late reply. But will be useful for some right.
Solution 3
I believe the file that you've mentioned in your post is the content of App.config file instead. It is the default configuration file for a number of C# project templates like WinForm application, console application, etc.
So three cases are possible in all:
If you want to use app.config file to hold the settings of
log4net
then you'll have to change the declaration in AssemlyInfo.cs file to:[assembly: log4net.Config.XmlConfigurator(Watch = true)]
In case you're using a dedicated settings file for
log4net
then you'll have to modify your Log4Net.config or Log4Net.xml file in a way thatlog4net
is the root tag of the config file:<log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="ConsoleAppender" /> </root> </log4net>
In this case you can keep your declaration in AssemblyInfo.cs file as it is that you've mentioned in your post:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
Now the final use-case. Let's say you're having a custom xml file of your own e.g. myCustomSettings.xml which contains
log4net
setting. A sample is as below:<MyCustomSetting> <mySection1></mySection1> <mySection2></mySection2> <log4net> <root> <level value="ALL" /> <appender-ref ref="file" /> <appender-ref ref="console" /> </root> <appender name="console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" /> </layout> <threshold value="Info" /> </appender> <appender name="file" type="log4net.Appender.RollingFileAppender"> <file value="${PROGRAMDATA}\MyProduct\myLogs.log" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <threshold value="Debug" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger - %message%newline" /> </layout> </appender> </log4net> </MyCustomSetting>
In such a case you'll have to configure
log4net
through C# code at run-time. Here is how you do it:using System.Xml; var configFilePath = "myCustomSettings.xml"; var log4netConfig = new XmlDocument(); log4netConfig.Load(File.OpenRead(configFilePath)); var repo = LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); var log4NetXml = log4netConfig["MyCustomSetting"]["log4net"]; log4net.Config.XmlConfigurator.Configure(repo, log4NetXml);
In this case you don't require the declarative configuration in AssemblyInfo.cs file. So make sure you've removed it from there:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Related videos on Youtube
Sebastian Müller
Financial Engineer, Software Engineer, Product Management, Mathematics, Finance, Insurance, IT Architect
Updated on April 27, 2022Comments
-
Sebastian Müller about 2 years
I tried to configure log4net for logging everything to the console output. I have a config file named
Log4Net.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="ConsoleAppender" /> </root> </log4net> </configuration>
and I have my main class (just a testing case)
namespace TestLog4Net { class Program { private static readonly ILog log = LogManager.GetLogger(typeof(Program)); static void Main(string[] args) { log.Info("Info"); log.Error("Error"); Console.ReadKey(); } } }
I added these lines to the
AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator( ConfigFile = "Log4Net.config", Watch = true)]
But now nothing is logged, can someone explain this?
-
Karthikeyan almost 13 yearsI am getting exactly same problem. I have similar implementation and still i am not able to fix the problem. Can you pls help me fix it ?
-
Saeed Raffoul over 10 yearsEnsure the log4net.config file is marked as “Copy To Output” -> “Copy Always” in Properties.
-
RBT over 2 yearsRelated post - Log4net xml output
-
-
Gwyneth Llewelyn about 2 yearsWhile probably correct, this explanation is minimalistic for C#/.NET newbies...
-
Gwyneth Llewelyn about 2 yearsAlso see the answer by @RBT for a much more thorough explanation on how to fix things.
-
Gwyneth Llewelyn about 2 yearsExcellent answer! I'm well aware that this question is old, but it seems that after well over a decade, it's still a nightmare to properly configure log4net. I'm aware that this question is ancient, and that the OP has probably left SO æons ago, but your answer ought to be promoted to be the correct solution.