My understanding is that separate instances of ILogger should share the current scope, even if those instances are created for different Types (or within other threads, other classes, or from other method calls). In our application, we create a separate ILogger<T> for each class that does logging, so it is important that the scope encompasses logging for all related work. When writing unit tests we need to verify this behavior including the scope. We are seeing the expected behavior with the normal AppInsights ILogger, but not when using MELT.
Testing the behavior using MELT, we see the scope attached only to the ILogger that began the scope. For example:
var loggerFactory = TestLoggerFactory.Create();
var logger1 = loggerFactory.CreateLogger<MyClass>();
var logger2 = loggerFactory.CreateLogger<MyOtherClass>();
using (logger1.BeginScope(new Dictionary<string, object> { { "property0", "value 0" } }))
{
logger1.LogInformation("log 1");
logger2.LogInformation("log 2");
}
If both loggers use the same class they share the scope, but as this example is written they do not. Checking loggerFactory.GetTestLoggerSink().LogEntries, the LogEntries created using "logger1" have a scope but the LogEntries from "logger2" do not have any scope. Is there any way to obtain the expected behavior?
My guess is that the current MELT behavior differs from other ILogger implementations because the current scope (AsyncLocal value) lives within each TestLogger, compared to other implementations which share the scope by storing it at the ILoggerFactory or ILoggerProvider level.
My understanding is that separate instances of ILogger should share the current scope, even if those instances are created for different Types (or within other threads, other classes, or from other method calls). In our application, we create a separate
ILogger<T>
for each class that does logging, so it is important that the scope encompasses logging for all related work. When writing unit tests we need to verify this behavior including the scope. We are seeing the expected behavior with the normal AppInsights ILogger, but not when using MELT.Testing the behavior using MELT, we see the scope attached only to the ILogger that began the scope. For example:
If both loggers use the same class they share the scope, but as this example is written they do not. Checking
loggerFactory.GetTestLoggerSink().LogEntries
, the LogEntries created using "logger1" have a scope but the LogEntries from "logger2" do not have any scope. Is there any way to obtain the expected behavior?My guess is that the current MELT behavior differs from other ILogger implementations because the current scope (AsyncLocal value) lives within each
TestLogger
, compared to other implementations which share the scope by storing it at theILoggerFactory
orILoggerProvider
level.