Ask*_* B. 8 c# logging entity-framework interceptor entity-framework-6
该IDbCommandInterceptor接口是不是非常有据可查.我只发现了一些稀缺的教程:
还有一些问题:
这些是关于挂钩的建议我发现:
1 - 静态DbInterception类:
DbInterception.Add(new MyCommandInterceptor());
Run Code Online (Sandbox Code Playgroud)
2 - 在DbConfiguration课堂上做上述建议
public class MyDBConfiguration : DbConfiguration {
public MyDBConfiguration() {
DbInterception.Add(new MyCommandInterceptor());
}
}
Run Code Online (Sandbox Code Playgroud)
3 - 使用配置文件:
<entityFramework>
<interceptors>
<interceptor type="EFInterceptDemo.MyCommandInterceptor, EFInterceptDemo"/>
</interceptors>
</entityFramework>
Run Code Online (Sandbox Code Playgroud)
虽然我无法弄清楚如何将DbConfiguration类挂钩到DbContext,并且既没有放入typeconfig方法的部分.我发现的另一个例子似乎建议您编写记录器的命名空间:
type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework"
Run Code Online (Sandbox Code Playgroud)
我注意到了DataBaseLogger工具IDisposable,IDbConfigurationInterceptor和
IDbInterceptor.IDbCommandInterceptor也实现了IDbInterceptor,所以我尝试(没有成功)格式化它像这样:
type="DataLayer.Logging.MyCommandInterceptor, DataLayer"
Run Code Online (Sandbox Code Playgroud)
当我DbInterception直接调用静态类时,它每次调用都会添加另一个拦截器.所以我的快速而肮脏的解决方案是利用静态构造函数:
//This partial class is a seperate file from the Entity Framework auto-generated class,
//to allow dynamic connection strings
public partial class MyDbContext // : DbContext
{
public Guid RequestGUID { get; private set; }
public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
DbContextListeningInitializer.EnsureListenersAdded();
RequestGUID = Guid.NewGuid();
//Database.Log = m => System.Diagnostics.Debug.Write(m);
}
private static class DbContextListeningInitializer
{
static DbContextListeningInitializer() //Threadsafe
{
DbInterception.Add(new MyCommandInterceptor());
}
//When this method is called, the static ctor is called the first time only
internal static void EnsureListenersAdded() { }
}
}
Run Code Online (Sandbox Code Playgroud)
但是,正确/有意的方法是什么?
我发现我的DbContext类只需要具有该DbConfigurationType属性,以便在运行时附加配置:
[DbConfigurationType(typeof(MyDBConfiguration))]
public partial class MyDbContext // : DbContext
{
public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{ }
}
public class MyDBConfiguration : DbConfiguration {
public MyDBConfiguration() {
this.AddInterceptor(new MyCommandInterceptor());
}
}
Run Code Online (Sandbox Code Playgroud)
该文档建议您可以将其放入Application_Start:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
DbInterception.Add(new SchoolInterceptorTransientErrors());
DbInterception.Add(new SchoolInterceptorLogging());
}
Run Code Online (Sandbox Code Playgroud)
重要的是它只被调用一次。
| 归档时间: |
|
| 查看次数: |
4446 次 |
| 最近记录: |