Unexpected behaviour of ILoggerFactory in EF Core
I need to add a logger for a specifc dbContext. For example I have the next code:
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>();
If I expand non-public members of loggerFactory1
I will see my CustomLoggerProvider that I added (_providerRegistrations = Count = 1). But If I expand non-public members of loggerFactory2
I will see the same CustomLoggerProvider (_providerRegistrations = Count = 1) although I didn't add it to dbContext2
.
Also I got another unexpected behaviour in the next code:
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>();
In this case I got 1 CustomLoggerProvider in dbContext3
(_providerRegistrations = Count = 1) and the same logger in dbContext4
(_providerRegistrations = Count = 1). So there is no relationship between dbContext1
, dbContext2
and dbContext3
, dbContext4
.
So if I add one more CustomLoggerProvider to dbContext1
I will get the next result:
dbContext1: 2 CustomLoggerProvider (_providerRegistrations = Count = 2)
dbContext2: 2 CustomLoggerProvider (_providerRegistrations = Count = 2)
dbContext3: 1 CustomLoggerProvider (_providerRegistrations = Count = 1)
dbContext4: 1 CustomLoggerProvider (_providerRegistrations = Count = 1)
What is the right way to work with ILoggerFactory?
entity-framework entity-framework-core
add a comment |
I need to add a logger for a specifc dbContext. For example I have the next code:
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>();
If I expand non-public members of loggerFactory1
I will see my CustomLoggerProvider that I added (_providerRegistrations = Count = 1). But If I expand non-public members of loggerFactory2
I will see the same CustomLoggerProvider (_providerRegistrations = Count = 1) although I didn't add it to dbContext2
.
Also I got another unexpected behaviour in the next code:
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>();
In this case I got 1 CustomLoggerProvider in dbContext3
(_providerRegistrations = Count = 1) and the same logger in dbContext4
(_providerRegistrations = Count = 1). So there is no relationship between dbContext1
, dbContext2
and dbContext3
, dbContext4
.
So if I add one more CustomLoggerProvider to dbContext1
I will get the next result:
dbContext1: 2 CustomLoggerProvider (_providerRegistrations = Count = 2)
dbContext2: 2 CustomLoggerProvider (_providerRegistrations = Count = 2)
dbContext3: 1 CustomLoggerProvider (_providerRegistrations = Count = 1)
dbContext4: 1 CustomLoggerProvider (_providerRegistrations = Count = 1)
What is the right way to work with ILoggerFactory?
entity-framework entity-framework-core
1
It will all make sense if you read this. The logger factory is a singleton (that is, if you initialized it as recommended).
– Gert Arnold
Jan 19 at 16:24
See EF Core documentation - Logging. It explicitly states thatILoggerFactory
is singleton and contains a warning "It is very important that applications do not create a new ILoggerFactory instance for each context instance. Doing so will result in a memory leak and poor performance.". In other words, logging per db context is not supported.
– Ivan Stoev
Jan 19 at 16:25
add a comment |
I need to add a logger for a specifc dbContext. For example I have the next code:
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>();
If I expand non-public members of loggerFactory1
I will see my CustomLoggerProvider that I added (_providerRegistrations = Count = 1). But If I expand non-public members of loggerFactory2
I will see the same CustomLoggerProvider (_providerRegistrations = Count = 1) although I didn't add it to dbContext2
.
Also I got another unexpected behaviour in the next code:
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>();
In this case I got 1 CustomLoggerProvider in dbContext3
(_providerRegistrations = Count = 1) and the same logger in dbContext4
(_providerRegistrations = Count = 1). So there is no relationship between dbContext1
, dbContext2
and dbContext3
, dbContext4
.
So if I add one more CustomLoggerProvider to dbContext1
I will get the next result:
dbContext1: 2 CustomLoggerProvider (_providerRegistrations = Count = 2)
dbContext2: 2 CustomLoggerProvider (_providerRegistrations = Count = 2)
dbContext3: 1 CustomLoggerProvider (_providerRegistrations = Count = 1)
dbContext4: 1 CustomLoggerProvider (_providerRegistrations = Count = 1)
What is the right way to work with ILoggerFactory?
entity-framework entity-framework-core
I need to add a logger for a specifc dbContext. For example I have the next code:
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>();
If I expand non-public members of loggerFactory1
I will see my CustomLoggerProvider that I added (_providerRegistrations = Count = 1). But If I expand non-public members of loggerFactory2
I will see the same CustomLoggerProvider (_providerRegistrations = Count = 1) although I didn't add it to dbContext2
.
Also I got another unexpected behaviour in the next code:
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>();
In this case I got 1 CustomLoggerProvider in dbContext3
(_providerRegistrations = Count = 1) and the same logger in dbContext4
(_providerRegistrations = Count = 1). So there is no relationship between dbContext1
, dbContext2
and dbContext3
, dbContext4
.
So if I add one more CustomLoggerProvider to dbContext1
I will get the next result:
dbContext1: 2 CustomLoggerProvider (_providerRegistrations = Count = 2)
dbContext2: 2 CustomLoggerProvider (_providerRegistrations = Count = 2)
dbContext3: 1 CustomLoggerProvider (_providerRegistrations = Count = 1)
dbContext4: 1 CustomLoggerProvider (_providerRegistrations = Count = 1)
What is the right way to work with ILoggerFactory?
entity-framework entity-framework-core
entity-framework entity-framework-core
asked Jan 19 at 16:10
user190794user190794
20218
20218
1
It will all make sense if you read this. The logger factory is a singleton (that is, if you initialized it as recommended).
– Gert Arnold
Jan 19 at 16:24
See EF Core documentation - Logging. It explicitly states thatILoggerFactory
is singleton and contains a warning "It is very important that applications do not create a new ILoggerFactory instance for each context instance. Doing so will result in a memory leak and poor performance.". In other words, logging per db context is not supported.
– Ivan Stoev
Jan 19 at 16:25
add a comment |
1
It will all make sense if you read this. The logger factory is a singleton (that is, if you initialized it as recommended).
– Gert Arnold
Jan 19 at 16:24
See EF Core documentation - Logging. It explicitly states thatILoggerFactory
is singleton and contains a warning "It is very important that applications do not create a new ILoggerFactory instance for each context instance. Doing so will result in a memory leak and poor performance.". In other words, logging per db context is not supported.
– Ivan Stoev
Jan 19 at 16:25
1
1
It will all make sense if you read this. The logger factory is a singleton (that is, if you initialized it as recommended).
– Gert Arnold
Jan 19 at 16:24
It will all make sense if you read this. The logger factory is a singleton (that is, if you initialized it as recommended).
– Gert Arnold
Jan 19 at 16:24
See EF Core documentation - Logging. It explicitly states that
ILoggerFactory
is singleton and contains a warning "It is very important that applications do not create a new ILoggerFactory instance for each context instance. Doing so will result in a memory leak and poor performance.". In other words, logging per db context is not supported.– Ivan Stoev
Jan 19 at 16:25
See EF Core documentation - Logging. It explicitly states that
ILoggerFactory
is singleton and contains a warning "It is very important that applications do not create a new ILoggerFactory instance for each context instance. Doing so will result in a memory leak and poor performance.". In other words, logging per db context is not supported.– Ivan Stoev
Jan 19 at 16:25
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54268958%2funexpected-behaviour-of-iloggerfactory-in-ef-core%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54268958%2funexpected-behaviour-of-iloggerfactory-in-ef-core%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It will all make sense if you read this. The logger factory is a singleton (that is, if you initialized it as recommended).
– Gert Arnold
Jan 19 at 16:24
See EF Core documentation - Logging. It explicitly states that
ILoggerFactory
is singleton and contains a warning "It is very important that applications do not create a new ILoggerFactory instance for each context instance. Doing so will result in a memory leak and poor performance.". In other words, logging per db context is not supported.– Ivan Stoev
Jan 19 at 16:25