-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugFileLoggerTest.cs
More file actions
58 lines (50 loc) · 1.83 KB
/
DebugFileLoggerTest.cs
File metadata and controls
58 lines (50 loc) · 1.83 KB
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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Open.Logging.Extensions.FileSystem;
namespace Open.Logging.Extensions.Tests;
public class DebugFileLoggerTest
{
[Fact]
public void TestFileLoggerConfiguration()
{
// Arrange
var tempDir = Path.Combine(Path.GetTempPath(), "DebugFileLoggerTest");
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["Logging:LogLevel:Default"] = "Debug",
["Logging:File:LogLevel:Default"] = "Debug",
["Logging:File:LogDirectory"] = tempDir,
["Logging:File:FileNamePattern"] = "debug-{Timestamp:yyyy-MM-dd-HH-mm-ss-fff}.log"
})
.Build();
var services = new ServiceCollection();
services.AddSingleton<IConfiguration>(configuration);
services.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddConfiguration(configuration.GetSection("Logging"));
builder.SetMinimumLevel(LogLevel.Debug);
builder.AddFileLogger();
});
using var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<DebugFileLoggerTest>>();
// Act
logger.LogInformation("Test message");
// Give time for async operations
System.Threading.Thread.Sleep(1000);
// Check for files
var logFiles = Directory.Exists(tempDir) ? Directory.GetFiles(tempDir, "*.log") : Array.Empty<string>();
System.Console.WriteLine($"Temp directory: {tempDir}");
System.Console.WriteLine($"Directory exists: {Directory.Exists(tempDir)}");
System.Console.WriteLine($"Found {logFiles.Length} log files");
foreach (var file in logFiles)
{
System.Console.WriteLine($"Log file: {file}");
}
// Assert
Assert.True(Directory.Exists(tempDir), "Log directory should be created");
Assert.NotEmpty(logFiles);
}
}