-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogTests.cs
83 lines (70 loc) · 2.73 KB
/
LogTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NET
using IntegrationTests.Helpers;
using OpenTelemetry.Proto.Logs.V1;
using Xunit.Abstractions;
namespace IntegrationTests;
public class LogTests : TestHelper
{
public LogTests(ITestOutputHelper output)
: base("Logs", output)
{
}
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
[Trait("Category", "EndToEnd")]
public void SubmitLogs(bool enableClrProfiler, bool includeFormattedMessage)
{
using var collector = new MockLogsCollector(Output);
SetExporter(collector);
if (includeFormattedMessage)
{
collector.Expect(logRecord => Convert.ToString(logRecord.Body) == "{ \"stringValue\": \"Information from Test App.\" }");
}
else
{
// When includeFormattedMessage is set to false
// LogRecord is not parsed and body will not have data.
// This is a default collector behavior.
collector.Expect(logRecord =>
{
var logsAsString = Convert.ToString(logRecord);
return logsAsString != null && logsAsString.Contains("TestApplication.Logs.Controllers.TestController");
});
}
if (enableClrProfiler)
{
EnableBytecodeInstrumentation();
}
else
{
SetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES", "OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper");
}
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOGS_INCLUDE_FORMATTED_MESSAGE", includeFormattedMessage.ToString());
RunTestApplication();
collector.AssertExpectations();
}
[Fact]
public async Task EnableLogsWithCLRAndHostingStartup()
{
using var collector = new MockLogsCollector(Output);
SetExporter(collector);
collector.ExpectCollected(ValidateSingleAppLogRecord, "App log record should be exported once.");
SetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES", "OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOGS_INCLUDE_FORMATTED_MESSAGE", "true");
EnableBytecodeInstrumentation();
RunTestApplication();
// wait for fixed amount of time for logs to be collected before asserting
await Task.Delay(TimeSpan.FromSeconds(10));
collector.AssertCollected();
}
private static bool ValidateSingleAppLogRecord(IEnumerable<LogRecord> records)
{
return records.Count(lr => Convert.ToString(lr.Body) == "{ \"stringValue\": \"Information from Test App.\" }") == 1;
}
}
#endif