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

1

3

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.

Ouster answered 28/3, 2020 at 5:20 Comment(4)
Are you able to debug the code of SetLog4NetConfiguration() and LogError methods?Propulsion
SetLog4NetConfiguration() goes fine without an exception thrown, and log.Error() method also debugs properly, but it did not make any entry in the DB.Ouster
#46671055 this might help youPropulsion
connectionStringName doesn't work properly for me. Using <connectionString value="Put your connectionstring here, no its name"/> in the log4net xml file does workVeracity
B
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

Boney answered 3/5, 2020 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.