I have a weird issue, my log4net setup isn't logging any data to the database nor it is raising any exceptions to notify the issue. I have defined the configuration settings in a separate file called "Log4net.Config" and have referenced it in the assembly. (Please Note, I installed the Log4net lib via nuget lib)
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
Here is the log File.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=(local);Initial Catalog=testdb;Integrated Security=True" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
</log4net>
</configuration>
I'm accessing it like:
log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
System.Console.ReadLine();
log.Debug("This is a debug object");
log.Error("ex", new Exception("dfdf"));
Do you see any issues?
log4net does not raise exceptions by design, the rationale is that an application should still run even if the logging configuration is wrong.
A standalone log4net configuration file should only contain the log4net configuration section, so your log4net.config file starts with this:
Also, you've defined appenders, but you haven't defined a logger, so you need to add something like this to log4net.config:
(The root logger is the default logger, you set the minimum log level and the appenders you want to use)
If you still have problems after fixing the file, set log4net into debug mode in the log4net.config file, and add a TraceListener section to your app.config (or web.config depending on project type):
in app.config:
When log4net initialises, it will write debugging information to the specified file.
Note that as you have used an assembly attribute to initialise logging, you might need to add a GetLogger call during your program startup: