Logging into Database using log4net and Microknights in asp.net core 3.1 didn't work

烈酒焚心 提交于 2021-01-01 04:51:14

问题


I have gone through many articles saying AdoNetAppender is not supported in .net core, but we can use MicroKnights to do the same.

I am trying to achieve DB logging in .net core 3.1 application using the same but still not succeed.

My log4net.config file placed in roots is as follows:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net debug="true">
<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection,System.Data.SqlClient,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />
      <connectionStringName value="log4net" />
      <connectionStringFile value="appsettings.json" />
      <commandText value="INSERT INTO ApplicationLog(ApplicationID, CreateDate, LogLevel, Message, Exception)
    VALUES (@ApplicationID, @CreateDate, @LogLevel, @Message, NULLIF(@Exception,''))" />
      <!--<commandText value="dbo.sp_InsertApplicationLog" />
      <commandType value="StoredProcedure" />-->
      <parameter name="ApplicationID">
        <parameterName value="@ApplicationID" />
        <dbType value="Int16" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{ApplicationID}" />
        </layout>
      </parameter>
      <parameter name="CreateDate">
        <parameterName value="@CreateDate" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter name="LogLevel">
        <parameterName value="@LogLevel" />
        <dbType value="AnsiString" />
        <size value="20" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter name="Message">
        <parameterName value="@Message" />
        <dbType value="AnsiString" />
        <size value="2000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter name="Exception">
        <parameterName value="@Exception" />
        <dbType value="AnsiString" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%exception" />
        </layout>
      </parameter>
      <threshold value="DEBUG"/>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="DEBUG" />
        <levelMax value="ERROR" />
      </filter>
    </appender>
    <!--<root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="AdoNetAppender" />
    </root>-->
    <root>
      <level value="ALL"/>
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="AdoNetAppender" />
    </root>
  </log4net>

</configuration>

My startup.cs file calling AddLog4Net() method.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddLog4Net();
}

And I am trying to log in database is like this,

private static readonly string LOG_CONFIG_FILE = @"log4net.config";
private static readonly ILog log = LogManager.GetLogger(typeof(Log4NetLoggingService));

private static void SetLog4NetConfiguration()
        {
            XmlDocument log4netConfig = new XmlDocument();
            log4netConfig.Load(File.OpenRead(LOG_CONFIG_FILE));

            var repo = LogManager.CreateRepository(
                Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));

            log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);

            AdoNetAppenderHelper.SetConnectionString(Configuration
                                .GetSection("Data:Logging")
                                .GetSection("DbConnectionString").Value);
        }

public void LogError(Exception exception)
        {

                var message = string.IsNullOrEmpty(exception.Source) ? exception.Message : exception.Source;

                SetLog4NetConfiguration();
                log.Error(message, exception);

        }

I cannot understand where I am wrong, but I tried my best but didn't succeed.


回答1:


For .Net Core 3.1, Microsoft has changed path for the SqlClient library. So you have to update the connectionType element inside the log4net.config to:

<connectionType value="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient, Version=1.0.0.0,Culture=neutral,PublicKeyToken=23ec7fc2d6eaa4a5"/>

You can find more details here microknights/Log4NetAdoNetAppender Github README.md



来源:https://stackoverflow.com/questions/60897550/logging-into-database-using-log4net-and-microknights-in-asp-net-core-3-1-didnt

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