在asp.net核心应用程序中配置log4net AdoNetAppender

时间:2019-01-04 07:24:22

标签: c# asp.net-core log4net

我正在尝试为我的asp.net核心应用程序使用MicroKnights AdoNetAppender(nuget第三方库)配置log4net。但是,什么也没有登录到数据库中。另外,在运行应用程序时我没有收到任何错误。我怀疑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"
  }
}

program.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();
        }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

尝试将debug属性添加到log4net根元素。

 @Bean
 public MessageChannel udpInboundChannel() {
      return new PublishSubscribeChannel();
 }

这将允许您在运行应用程序时查看调试输出。我对您提供的示例进行了此操作,它报告了连接类型错误。错误是

<log4net debug="true">

我怀疑您是否更改此行

log4net: Setting Collection Property [AddParameter] to object [MicroKnights.Logging.AdoNetAppenderParameter]
log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Failed to load connection type [System.Data.SqlClient.SqlConnection, System.Data, Version=4.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]
System.IO.FileLoadException: Could not load file or assembly 'System.Data, Version=4.4.0.65535, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Data, Version=4.4.0.65535, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type, ObjectHandleOnStack keepalive)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
   at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
   at MicroKnights.Logging.AdoNetAppender.ResolveConnectionType()

<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

它将起作用。 MicroKnights的版本1.0.2中进行了更改,以使用可用的System.Data.SqlClient版本。