Skip to content

Commit

Permalink
Merge branch 'main' into sdk-logs-builderextensions
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Apr 27, 2023
2 parents 3caf19a + 6c9c48f commit 3d53fd7
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System.Diagnostics;
using System.Text;
using OpenTelemetry.Tests;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -41,14 +42,15 @@ public void SelfDiagnosticsConfigRefresher_OmitAsConfigured()
{
try
{
CreateConfigFile();
string logDirectory = Utils.GetCurrentMethodName();
CreateConfigFile(logDirectory);
using var configRefresher = new SelfDiagnosticsConfigRefresher();

// Emitting event of EventLevel.Warning
OpenTelemetrySdkEventSource.Log.ObservableInstrumentCallbackException("exception");

int bufferSize = 512;
byte[] actualBytes = ReadFile(bufferSize);
byte[] actualBytes = ReadFile(logDirectory, bufferSize);
string logText = Encoding.UTF8.GetString(actualBytes);
this.output.WriteLine(logText); // for debugging in case the test fails
Assert.StartsWith(MessageOnNewFileString, logText);
Expand All @@ -67,15 +69,16 @@ public void SelfDiagnosticsConfigRefresher_CaptureAsConfigured()
{
try
{
CreateConfigFile();
string logDirectory = Utils.GetCurrentMethodName();
CreateConfigFile(logDirectory);
using var configRefresher = new SelfDiagnosticsConfigRefresher();

// Emitting event of EventLevel.Error
OpenTelemetrySdkEventSource.Log.TracerProviderException("Event string sample", "Exception string sample");
string expectedMessage = "Unknown error in TracerProvider '{0}': '{1}'.{Event string sample}{Exception string sample}";

int bufferSize = 2 * (MessageOnNewFileString.Length + expectedMessage.Length);
byte[] actualBytes = ReadFile(bufferSize);
byte[] actualBytes = ReadFile(logDirectory, bufferSize);
string logText = Encoding.UTF8.GetString(actualBytes);
Assert.StartsWith(MessageOnNewFileString, logText);

Expand All @@ -97,24 +100,24 @@ private static string ParseLogMessage(string logLine)
return logLine.Substring(timestampPrefixLength);
}

private static byte[] ReadFile(int byteCount)
private static byte[] ReadFile(string logDirectory, int byteCount)
{
var outputFileName = Path.GetFileName(Process.GetCurrentProcess().MainModule?.FileName) + "."
+ Process.GetCurrentProcess().Id + ".log";
var outputFilePath = Path.Combine(".", outputFileName);
var outputFilePath = Path.Combine(logDirectory, outputFileName);
using var file = File.Open(outputFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
byte[] actualBytes = new byte[byteCount];
_ = file.Read(actualBytes, 0, byteCount);
return actualBytes;
}

private static void CreateConfigFile()
private static void CreateConfigFile(string logDirectory)
{
string configJson = @"{
""LogDirectory"": ""."",
string configJson = $@"{{
""LogDirectory"": ""{logDirectory}"",
""FileSize"": 1024,
""LogLevel"": ""Error""
}";
}}";
using FileStream file = File.Open(ConfigFilePath, FileMode.Create, FileAccess.Write);
byte[] configBytes = Encoding.UTF8.GetBytes(configJson);
file.Write(configBytes, 0, configBytes.Length);
Expand Down

0 comments on commit 3d53fd7

Please sign in to comment.