Skip to content

Commit

Permalink
clenaup test, changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Ting committed Oct 17, 2023
1 parent 39d0107 commit dc66b62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

[TestClass]
Expand Down Expand Up @@ -39,7 +40,8 @@ public void SelfDiagnosticsConfigRefresher_OmitAsConfigured()

// The event was omitted
Assert.AreEqual('\0', (char)actualBytes[MessageOnNewFile.Length]);
}}
}
}
finally
{
CleanupConfigFile();
Expand Down Expand Up @@ -80,19 +82,13 @@ public void SelfDiagnosticsConfigRefresher_CaptureAsConfigured()
[TestMethod]
public void SelfDiagnosticsConfigRefresher_ReadFromEnviornmentVar()
{

var key = "APPLICATIONINSIGHTS_LOG_DIAGNOSTICS";
var val = "/home/user/LogFiles/SelfDiagnostics";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
val = @"C:\home\LogFiles\SelfDiagnostics";
}

var val = @"C:\home\LogFiles\SelfDiagnostics";
Environment.SetEnvironmentVariable(key, val);

try
{
CreateConfigFile(false);
CreateConfigFile(false, val);
using (var configRefresher = new SelfDiagnosticsConfigRefresher())
{
// Emitting event of EventLevel.Error
Expand Down Expand Up @@ -136,32 +132,38 @@ private static byte[] ReadFile(string filePath, int byteCount)
}
}

private static void CreateConfigFile(bool userDefinedLogDirectory = true)
private void CreateConfigFile(bool userDefinedLogDirectory = true, string envVarVal = "")
{
string configJson = string.Empty;
if (!userDefinedLogDirectory)
ConfigFileObj configFileObj = new()
{
FileSize = 1024,
LogLevel = "Error"
};

if (userDefinedLogDirectory)
{
configJson = @"{
""FileSize"": 1024,
""LogLevel"": ""Error""
}";
configFileObj.LogDirectory = ".";
}
else
{
configJson = @"{
""LogDirectory"": ""."",
""FileSize"": 1024,
""LogLevel"": ""Error""
}";
configFileObj.LogDirectory = envVarVal;
}

string configJson = JsonConvert.SerializeObject(configFileObj);
using (FileStream file = File.Open(ConfigFilePath, FileMode.Create, FileAccess.Write))
{
byte[] configBytes = Encoding.UTF8.GetBytes(configJson);
file.Write(configBytes, 0, configBytes.Length);
}
}

private class ConfigFileObj
{
public int FileSize { get; set; }
public string LogLevel { get; set; }
public string LogDirectory { get; set; }
};

private static void CleanupConfigFile()
{
try
Expand All @@ -173,4 +175,4 @@ private static void CleanupConfigFile()
}
}
}
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## VNext
- [Added support to read diagnostics config from the path defined in environment variable.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/2769)

## Version 2.22.0-beta3
- [Do not report CosmosDB transport-level calls](https://github.com/microsoft/ApplicationInsights-dotnet/pull/2789)
Expand Down

0 comments on commit dc66b62

Please sign in to comment.