log4net not logging to the database

梦想的初衷 提交于 2019-12-10 20:28:39

问题


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?


回答1:


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:

<?xml version="1.0" standalone="yes"?>
<log4net>
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="1" /> …

Also, you've defined appenders, but you haven't defined a logger, so you need to add something like this to log4net.config:

<root>
  <level value="DEBUG" />
  <appender-ref ref="AdoNetAppender" />
</root>

(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):

<log4net debug="true">
      <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> …

in app.config:

<system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add
            name="textWriterTraceListener"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="c:\temp\log4net.txt" />
      </listeners>
    </trace>
  </system.diagnostics>

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:

Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.



来源:https://stackoverflow.com/questions/24837461/log4net-not-logging-to-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!