I have a problem with the AdoNetAppender of log4net in my ASP.NET Core application (netcoreapp1.1).
If I want to use the AdoNetAppender, I get the following error:
System.TypeLoadException: Could not load type 'log4net.Appender.AdoNetAppender' from assembly 'log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a'.
Is there anybody who have the AdoNetAppender running under ASP.NET Core (netcoreapp1.1)? If not, is there a good alternative to log with log4net into a database? I also use Entity Framework Core.
The AdoNetAppender
isn't supported at present on .NET Core 1.0 / .NET Standard 1.3: https://logging.apache.org/log4net/release/framework-support.html#Appenders
For .Net Core you can use nuget.org/packages/MicroKnights.Log4NetAdoNetAppender . It has a bit differences in set up compare to AdoNetAppender, but is very straightforward too, to Find how to set it up here: github.com/microknights/Log4NetAdoNetAppender
Here an example that works for me
<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>
and in your appsettings.json
"ConnectionStrings": {
"log4net": "Data Source=.\\SQLEXPRESS;Initial Catalog=LogDB;User ID=user;Password=pass",
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With