我的ASP.NET核心应用程序(netcoreapp1.1)中的log4net的AdoNetAppender出现问题。
如果我想使用AdoNetAppender,我会收到以下错误:
System.TypeLoadException:无法从程序集'log4net,Version = 2.0.8.0,Culture = neutral,PublicKeyToken = 669e0ddf0bb1aa2a'加载类型'log4net.Appender.AdoNetAppender'。
是否有人在ASP.NET Core(netcoreapp1.1)下运行AdoNetAppender?如果没有,是否有一个很好的替代方法可以将log4net登录到数据库中?我也使用Entity Framework Core。
答案 0 :(得分:4)
目前在.NET Core 1.0 / .NET Standard 1.3上不支持AdoNetAppender
:https://logging.apache.org/log4net/release/framework-support.html#Appenders
答案 1 :(得分:0)
对于.Net Core,您可以使用nuget.org/packages/MicroKnights.Log4NetAdoNetAppender。与AdoNetAppender相比,它的设置略有不同,但是在此处查找如何设置它也非常简单:github.com/microknights/Log4NetAdoNetAppender
这里有一个适合我的示例
<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 Log ....." />
<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>
并在您的appsettings.json
中"ConnectionStrings": {
"log4net": "Data Source=.\\SQLEXPRESS;Initial Catalog=LogDB;User ID=user;Password=pass",
}