我正在尝试使用MicroKnights AdoNetAppender(nuget第三方库)配置log4net,用于我的asp.net核心应用程序。但是,数据库中没有记录任何内容。而且在运行应用程序时没有出现任何错误。我怀疑connectionType是导致问题的原因。
log4net配置文件:
<log4net>
<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<connectionStringName value="log4net" />
<connectionStringFile value="appsettings.json" />
<commandText value="INSERT INTO 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="ALL" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"connectionStrings": {
"log4net": "Server=[[ServerName]]; Database=[[DatabaseName]]; Trusted_Connection=True; MultipleActiveResultSets=true"
}
}
程序.cs中的主方法
public static void Main(string[] args)
{
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
ILog log = log4net.LogManager.GetLogger("Logger");
log.Info("logging to database");
CreateWebHostBuilder(args).Build().Run();
}
任何帮助都将不胜感激。
typeof(SqlConnection).AssemblyQualifiedName
找到了正确的 ConnectionType,并使用其输出。请参见:https://github.com/microknights/Log4NetAdoNetAppender/issues/1#issuecomment-385940980 - carraua