ASP .Net Core 3.0 - Log4Net MS SQL Server Appender 不工作
ASP .Net Core 3.0 - Log4Net MS SQL Server Appender not working
我目前正在尝试使用 Log4Net 将日志插入到 MS SQL 服务器数据库中。我相信我至少有 Log4Net 工作,因为我能够使用给定 Here and I was able to generate a file with the stack traces. However, 8 hours of research and tutorials has me totally stomped on getting the MS SQL Server config examples given Here - same place 的 FileAppender option/example 工作。
我什至无法让他们的例子发挥作用。我创建了 table,我将我的 log4net.config 文件从 FileAppender 选项更新为提供的 MS SQL 服务器选项,我给它提供了正确的连接字符串设置......没有。
我将 post 我认为会有所帮助的代码花絮和一张证明我的数据库 table 确实存在的照片。
配置文件
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=[].database.windows.net;initial catalog=[];integrated security=false;persist security info=True;User ID=[];Password=[]" />
<commandText value="INSERT INTO dbo.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>
<root>
<level value="ALL"></level>
<appender-ref ref="AdoNetAppender"></appender-ref>
</root>
</appender>
这是我制作的一种方法,只是为了让它爆炸并尝试记录一些东西。
public IActionResult Boom()
{
// created a boom method just to MAKE it log no matter what.. but no logging happens
_logger.LogDebug("Testing Debug log");
throw new Exception("Boom, HA HA HA");
}
这是我的Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddLog4Net();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
我花了很多时间尝试不同的方法,下面是我如何以最少的更改使其工作。
首先,安装 NuGet 包 MicroKnights.Log4NetAdoNetAppender
然后将您的 log4net.config
文件更新为如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
<bufferSize value="1" />
<connectionType value="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient, Version=1.0.0.0,Culture=neutral,PublicKeyToken=23ec7fc2d6eaa4a5"/>
<connectionStringName value="LocalDB" />
<connectionStringFile value="appsettings.json" />
<commandText value="INSERT INTO dbo.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>
<root>
<level value="Info" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
注意:
- 对于 .Net Core 3.1+
,connectionType
的值需要像这样
connectionStringName
指向您连接字符串的名称
connectionStringFile
是您的 JSON 配置文件的名称
所以只需要更改 log4net.config
和安装 NuGet 包。之后,您将在数据库中获得日志:
有关详细信息,请查看 NuGet 包 GitHub page。
我目前正在尝试使用 Log4Net 将日志插入到 MS SQL 服务器数据库中。我相信我至少有 Log4Net 工作,因为我能够使用给定 Here and I was able to generate a file with the stack traces. However, 8 hours of research and tutorials has me totally stomped on getting the MS SQL Server config examples given Here - same place 的 FileAppender option/example 工作。
我什至无法让他们的例子发挥作用。我创建了 table,我将我的 log4net.config 文件从 FileAppender 选项更新为提供的 MS SQL 服务器选项,我给它提供了正确的连接字符串设置......没有。
我将 post 我认为会有所帮助的代码花絮和一张证明我的数据库 table 确实存在的照片。
配置文件
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=[].database.windows.net;initial catalog=[];integrated security=false;persist security info=True;User ID=[];Password=[]" />
<commandText value="INSERT INTO dbo.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>
<root>
<level value="ALL"></level>
<appender-ref ref="AdoNetAppender"></appender-ref>
</root>
</appender>
这是我制作的一种方法,只是为了让它爆炸并尝试记录一些东西。
public IActionResult Boom()
{
// created a boom method just to MAKE it log no matter what.. but no logging happens
_logger.LogDebug("Testing Debug log");
throw new Exception("Boom, HA HA HA");
}
这是我的Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddLog4Net();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
我花了很多时间尝试不同的方法,下面是我如何以最少的更改使其工作。
首先,安装 NuGet 包 MicroKnights.Log4NetAdoNetAppender
然后将您的 log4net.config
文件更新为如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
<bufferSize value="1" />
<connectionType value="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient, Version=1.0.0.0,Culture=neutral,PublicKeyToken=23ec7fc2d6eaa4a5"/>
<connectionStringName value="LocalDB" />
<connectionStringFile value="appsettings.json" />
<commandText value="INSERT INTO dbo.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>
<root>
<level value="Info" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
注意:
- 对于 .Net Core 3.1+ ,
connectionStringName
指向您连接字符串的名称connectionStringFile
是您的 JSON 配置文件的名称
connectionType
的值需要像这样
所以只需要更改 log4net.config
和安装 NuGet 包。之后,您将在数据库中获得日志:
有关详细信息,请查看 NuGet 包 GitHub page。