-
Notifications
You must be signed in to change notification settings - Fork 770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get LogRecord.ScopeValues after end of scope #3304
Comments
@ramkumarchan Can you give me a little more information about what you are trying to accomplish? Are trying to write tests or something more real like an exporter?
What the tests are doing is looping over the scopes and adding them to a list... List<object> scopes = new List<object>();
logRecord.ForEachScope<object>(
(scope, state) =>
{
scopes.Add(scope.Scope);
},
null); So if you call |
@CodeBlanch I am trying to write simple unit test to assert the scope tag values. Below is a sample code of my unit test. Assuming LogInfo() method cannot modified, how to assert scope tags ?
|
So scopes are only available to processors during the If you want to get it working before that, try something like this... [Fact]
public void VerifyScopes()
{
var exporter = new TestExporter();
using (var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = true;
options.AddProcessor(new BatchLogRecordExportProcessor(exporter));
});
}))
{
var logger = loggerFactory.CreateLogger("test");
LogInfo(logger);
} // Dispose here causes the batch to flush to exporter
List<object> scopes = new List<object>();
exporter.ExportedItems[0].ForEachScope<object>(
(scope, state) =>
{
scopes.Add(scope.Scope);
},
null);
Assert.Single(scopes);
Dictionary<string, object> scope = scopes[0] as Dictionary<string, object>;
Assert.NotNull(scope);
Assert.Equal(12345, scope["contextId"]);
}
private void LogInfo(ILogger logger)
{
var tags = new Dictionary<string, object>() { ["contextId"] = 12345 };
using var scope = logger.BeginScope(tags);
logger.LogInformation("OpenTelemetry!");
}
private sealed class TestExporter : BaseExporter<LogRecord>
{
public List<LogRecord> ExportedItems { get; } = new();
public override ExportResult Export(in Batch<LogRecord> batch)
{
foreach (LogRecord logRecord in batch)
{
this.ExportedItems.Add(logRecord);
}
return ExportResult.Success;
}
} |
Great ! Thanks for the explanation @CodeBlanch ! |
Hello,
In test VerifyIncludeScopes_True in LogRecordTest.cs, the scope key value pairs are verified before the scope ends. Is it possible to obtain and verify the scope key value pairs after the scope has ended ?
Existing :
Question :
The text was updated successfully, but these errors were encountered: