|
5 | 5 |
|
6 | 6 | using System; |
7 | 7 | using System.IO; |
| 8 | +using System.Runtime.InteropServices; |
8 | 9 | using Datadog.Trace.Configuration; |
9 | 10 | using Datadog.Trace.Configuration.Telemetry; |
10 | 11 | using Datadog.Trace.Logging; |
@@ -197,4 +198,161 @@ public void WhenConsoleSinkIsNotIncluded_DoesNotUseConsoleSink(string sinks) |
197 | 198 | config.Console.Should().BeNull(); |
198 | 199 | } |
199 | 200 | } |
| 201 | + |
| 202 | + public class GetDefaultLogDirectoryTests |
| 203 | + { |
| 204 | + [Fact] |
| 205 | + public void WithNoEnvironmentVariables_ReturnsDefaultDirectory() |
| 206 | + { |
| 207 | + var source = new NameValueConfigurationSource(new()); |
| 208 | + var result = DatadogLoggingFactory.GetDefaultLogDirectory(source, NullConfigurationTelemetry.Instance); |
| 209 | + |
| 210 | + result.Should().NotBeNullOrEmpty(); |
| 211 | + |
| 212 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 213 | + { |
| 214 | + result.Should().EndWith(@"Datadog .NET Tracer\logs"); |
| 215 | + } |
| 216 | + else |
| 217 | + { |
| 218 | + result.Should().Be("/var/log/datadog/dotnet"); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + [Fact] |
| 223 | + public void WithAzureAppServices_ReturnsAzureLogDirectory() |
| 224 | + { |
| 225 | + var source = new NameValueConfigurationSource( |
| 226 | + new() |
| 227 | + { |
| 228 | + { "WEBSITE_SITE_NAME", "my-site-name" } |
| 229 | + }); |
| 230 | + |
| 231 | + var result = DatadogLoggingFactory.GetDefaultLogDirectory(source, NullConfigurationTelemetry.Instance); |
| 232 | + result.Should().NotBeNullOrEmpty(); |
| 233 | + |
| 234 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 235 | + { |
| 236 | + result.Should().Be(@"C:\home\LogFiles\datadog"); |
| 237 | + } |
| 238 | + else |
| 239 | + { |
| 240 | + result.Should().Be("/home/LogFiles/datadog"); |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + public class GetProgramDataDirectoryTests |
| 246 | + { |
| 247 | + [Fact] |
| 248 | + public void ReturnsNonEmptyDirectory() |
| 249 | + { |
| 250 | + // Skip on non-Windows platforms since this method is only called on Windows |
| 251 | + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 252 | + { |
| 253 | + return; |
| 254 | + } |
| 255 | + |
| 256 | + var result = DatadogLoggingFactory.GetProgramDataDirectory(); |
| 257 | + |
| 258 | + result.Should().NotBeNullOrEmpty(); |
| 259 | + result.Should().NotBe(string.Empty); |
| 260 | + } |
| 261 | + |
| 262 | + [Fact] |
| 263 | + public void OnWindows_ReturnsValidProgramDataPath() |
| 264 | + { |
| 265 | + // Skip on non-Windows platforms |
| 266 | + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 267 | + { |
| 268 | + return; |
| 269 | + } |
| 270 | + |
| 271 | + var result = DatadogLoggingFactory.GetProgramDataDirectory(); |
| 272 | + |
| 273 | + // Should be a rooted path (e.g., C:\ProgramData) |
| 274 | + Path.IsPathRooted(result).Should().BeTrue(); |
| 275 | + |
| 276 | + // Should contain "ProgramData" or "Program Data" (for localized versions) |
| 277 | + // or be the fallback C:\ProgramData |
| 278 | + (result.Contains("ProgramData", StringComparison.OrdinalIgnoreCase) || |
| 279 | + result.Contains("Program Data", StringComparison.OrdinalIgnoreCase) || |
| 280 | + result.Equals(@"C:\ProgramData", StringComparison.OrdinalIgnoreCase)) |
| 281 | + .Should().BeTrue(); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + public class TryCreateLogDirectoryTests |
| 286 | + { |
| 287 | + [Fact] |
| 288 | + public void WithValidPath_CreatesDirectoryAndReturnsTrue() |
| 289 | + { |
| 290 | + var logDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 291 | + Directory.Exists(logDirectory).Should().BeFalse(); |
| 292 | + |
| 293 | + var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory); |
| 294 | + |
| 295 | + result.Should().BeTrue(); |
| 296 | + Directory.Exists(logDirectory).Should().BeTrue(); |
| 297 | + |
| 298 | + // Cleanup |
| 299 | + try |
| 300 | + { |
| 301 | + Directory.Delete(logDirectory); |
| 302 | + } |
| 303 | + catch |
| 304 | + { |
| 305 | + // Ignore cleanup errors |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + [Fact] |
| 310 | + public void WithExistingDirectory_ReturnsTrue() |
| 311 | + { |
| 312 | + var logDirectory = Path.GetTempPath(); |
| 313 | + Directory.Exists(logDirectory).Should().BeTrue(); |
| 314 | + |
| 315 | + var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory); |
| 316 | + |
| 317 | + result.Should().BeTrue(); |
| 318 | + } |
| 319 | + |
| 320 | + [Fact] |
| 321 | + public void WithInvalidPath_ReturnsFalse() |
| 322 | + { |
| 323 | + // Use an invalid path that cannot be created |
| 324 | + var logDirectory = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) |
| 325 | + ? @"Z:\nonexistent\invalid\path\that\cannot\be\created" |
| 326 | + : "/root/nonexistent/invalid/path/that/cannot/be/created"; |
| 327 | + |
| 328 | + var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory); |
| 329 | + |
| 330 | + result.Should().BeFalse(); |
| 331 | + } |
| 332 | + |
| 333 | + [Fact] |
| 334 | + public void WithNestedPath_CreatesAllDirectories() |
| 335 | + { |
| 336 | + var parentDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 337 | + var logDirectory = Path.Combine(parentDir, "nested", "log", "directory"); |
| 338 | + Directory.Exists(logDirectory).Should().BeFalse(); |
| 339 | + Directory.Exists(parentDir).Should().BeFalse(); |
| 340 | + |
| 341 | + var result = DatadogLoggingFactory.TryCreateLogDirectory(logDirectory); |
| 342 | + |
| 343 | + result.Should().BeTrue(); |
| 344 | + Directory.Exists(logDirectory).Should().BeTrue(); |
| 345 | + Directory.Exists(parentDir).Should().BeTrue(); |
| 346 | + |
| 347 | + // Cleanup |
| 348 | + try |
| 349 | + { |
| 350 | + Directory.Delete(parentDir, recursive: true); |
| 351 | + } |
| 352 | + catch |
| 353 | + { |
| 354 | + // Ignore cleanup errors |
| 355 | + } |
| 356 | + } |
| 357 | + } |
200 | 358 | } |
0 commit comments