ILoggerFactory在EF Core中的意外行为

时间:2019-01-19 16:10:58

标签: entity-framework entity-framework-core

我需要为特定的dbContext添加一个记录器。例如,我有下一个代码:

'{"detalle": "el Expediente N\\\\u00b0\\\\u00a030 de la Resoluci\\\\u00f3n 11..."}'

如果我展开var dbContextOptionsBuilder1 = new DbContextOptionsBuilder(); dbContextOptionsBuilder1.UseSqlServer("Data Source=localhost;Initial Catalog=DataBase;Integrated Security=True"); var dbContext1 = new DbContext(dbContextOptionsBuilder1.Options); var loggerFactory1 = dbContext1.GetService<ILoggerFactory>(); loggerFactory1.AddProvider(new CustomLoggerProvider()); var dbContextOptionsBuilder2 = new DbContextOptionsBuilder(); dbContextOptionsBuilder2.UseSqlServer("Data Source=localhost;Initial Catalog=DataBase;Integrated Security=True"); var dbContext2 = new DbContext(dbContextOptionsBuilder2.Options); var loggerFactory2 = dbContext2.GetService<ILoggerFactory>(); 的非公开成员,我会看到我添加的CustomLoggerProvider(_providerRegistrations = Count = 1)。但是,如果我展开loggerFactory1的非公开成员,尽管我没有将其添加到loggerFactory2,但我会看到相同的CustomLoggerProvider(_providerRegistrations = Count = 1)。

在下一个代码中,我还出现了另一个意外行为:

dbContext2

在这种情况下,我在public class MyDbContext : DbContext { private readonly IDbService dbService; public MyDbContext(IDbService dbService, DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { } } var dbContextOptionsBuilder1 = new DbContextOptionsBuilder(); dbContextOptionsBuilder1.UseSqlServer("Data Source=localhost;Initial Catalog=DataBase;Integrated Security=True"); var dbContext1 = new DbContext(dbContextOptionsBuilder1.Options); var loggerFactory1 = dbContext1.GetService<ILoggerFactory>(); loggerFactory1.AddProvider(new CustomLoggerProvider()); var dbContextOptionsBuilder2 = new DbContextOptionsBuilder(); dbContextOptionsBuilder2.UseSqlServer("Data Source=localhost;Initial Catalog=DataBase;Integrated Security=True"); var dbContext2 = new DbContext(dbContextOptionsBuilder2.Options); var loggerFactory2 = dbContext2.GetService<ILoggerFactory>(); var dbContextOptionsBuilder3 = new DbContextOptionsBuilder(); dbContextOptionsBuilder3.UseSqlServer("Data Source=localhost;Initial Catalog=DataBase;Integrated Security=True"); var dbContext3 = new MyDbContext(new DbService(), dbContextOptionsBuilder3.Options); var loggerFactory3 = dbContext3.GetService<ILoggerFactory>(); loggerFactory3.AddProvider(new CustomLoggerProvider()); var dbContextOptionsBuilder4 = new DbContextOptionsBuilder(); dbContextOptionsBuilder4.UseSqlServer("Data Source=localhost;Initial Catalog=DataBase;Integrated Security=True"); var dbContext4 = new MyDbContext(new DbService(), dbContextOptionsBuilder4.Options); var loggerFactory4 = dbContext4.GetService<ILoggerFactory>(); 中获得了1个CustomLoggerProvider(_providerRegistrations = Count = 1),在dbContext3中获得了相同的记录器(_providerRegistrations = Count = 1)。因此,dbContext4dbContext1dbContext2dbContext3之间没有关系。 因此,如果我向dbContext4添加一个CustomLoggerProvider,我将得到下一个结果:

dbContext1

使用ILoggerFactory的正确方法是什么?

0 个答案:

没有答案