如何在log4net中使用实体框架连接字符串



有没有办法配置log4net的连接字符串并使用实体框架的相同连接字符串?

这是我的实体框架连接字符串:

<add name="EntityFrameworkConnection" connectionString="metadata=res://*/Data.BinAppModel.csdl|res://*/Data.BinAppModel.ssdl|res://*/Data.BinAppModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.sqlexpress;initial catalog=database;user id=user;password=pwd;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />   

这是我的log4net配置

<connectionType value="System.Data.SqlClient.SqlConnection, System.Data,  Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionStringName value="EntityFrameworkConnection" />

但我得到了这个错误:

log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Could not open database connection [metadata=res://*/Data.BinAppModel.csdl|res://*/Data.BinAppModel.ssdl|res://*/Data.BinAppModel.msl;provider=System.Data.SqlClient;provider connection string="data source=data source=.sqlexpress;initial catalog=database;user id=user;password=pwd;multipleactiveresultsets=True;application name=EntityFramework&quot;"]. Connection string context [ConnectionStringName].

如果您愿意做一些编码,可以执行以下操作:

  1. 创建一个从log4net.Appender.AdoNetAppender继承的简单类并重写方法CreateConnection
  2. 更改log4net配置以使用新的EFNetAppender类
  3. 更改log4net"connectionType"元素以使用EF连接工厂
  4. 将配置中的任何其他位置更改为EFNetAppender

EFNetAppender类:

using System;
using System.Data;
using System.Data.Entity.Infrastructure;
using log4net;
namespace YourNamespace
{
   public class EFNetAppender : log4net.Appender.AdoNetAppender
   {
      protected override IDbConnection CreateConnection(Type connectionType, string connectionString)
      {
          var factory = (IDbConnectionFactory)Activator.CreateInstance(connectionType);
          IDbConnection instance = factory.CreateConnection(connectionString);
          instance.ConnectionString = connectionString;
          return instance;
      }
   }
}

Log4Net配置:

<log4net>
  <appender name="EFNetAppender" type="YourNamespace.EFNetAppender">
     <connectionType value="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     <connectionStringName value="EntityFrameworkConnection" />
     ...
     ...
  <appender>
  <root>
      ...
      <appender-ref ref="EFNetAppender" />
      ...
  </root>

我用EF版本6进行了测试。

继续@Klinger的回答:

我遇到了一个由EntityFramework connectionString(与OP中一样具有元数据的连接字符串)引起的问题。尝试时引发异常使用有关元数据而非关键字的消息创建连接。

我要做的是提取提供者的连接字符串,并将其传递给两个工厂。CreateConnection函数和实例。ConnectionString属性,如下面的代码所示。

using System;
using System.Data;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Infrastructure;
namespace AdvancedPackagesTracker.DataAccess
{
    public class EFNetAppender : log4net.Appender.AdoNetAppender
    {
        protected override IDbConnection CreateConnection(Type connectionType, string connectionString)
        {
            var factory = (IDbConnectionFactory)Activator.CreateInstance(connectionType);
            var builder = new EntityConnectionStringBuilder();
            var providerConnectionString = new EntityConnectionStringBuilder(connectionString).ProviderConnectionString;
            IDbConnection instance = factory.CreateConnection(providerConnectionString);
            instance.ConnectionString = providerConnectionString;
            return instance;
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新