在核心 3.1 中使用 log4net 和 Microknights 登录数据库 asp.net 不起作用



我看过很多文章,说.net核心不支持AdoNetAppender,但我们可以使用MicroKnights来做同样的事情。

我正在尝试使用相同的方法在.net core 3.1应用程序中实现DB登录,但仍然没有成功。

放置在根目录中的log4net.config文件如下:

<?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>

我的startup.cs文件调用AddLog4Net((方法。

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

我正在尝试登录数据库,是这样的

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);
}

我不明白我错在哪里,但我尽力了,但没有成功。

对于.Net Core 3.1,Microsoft已更改SqlClient库的路径。因此,您必须将log4net.config中的connectionType元素更新为:

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

您可以在这里找到更多详细信息microknights/Log4NetAdoNetAppender Github README.md

相关内容

  • 没有找到相关文章

最新更新