cs-codex-dist-tests/Logging/FixtureLog.cs

55 lines
1.5 KiB
C#
Raw Normal View History

2023-04-14 12:53:39 +00:00
using NUnit.Framework;
namespace Logging
{
public class FixtureLog : BaseLog
{
private readonly DateTime start;
private readonly string fullName;
2023-04-25 09:31:15 +00:00
private readonly LogConfig config;
2023-04-14 12:53:39 +00:00
public FixtureLog(LogConfig config, string name = "")
2023-04-25 09:31:15 +00:00
: base(config.DebugEnabled)
2023-04-14 12:53:39 +00:00
{
start = DateTime.UtcNow;
var folder = DetermineFolder(config);
var fixtureName = GetFixtureName(name);
2023-04-14 12:53:39 +00:00
fullName = Path.Combine(folder, fixtureName);
2023-04-25 09:31:15 +00:00
this.config = config;
2023-04-14 12:53:39 +00:00
}
public TestLog CreateTestLog(string name = "")
2023-04-14 12:53:39 +00:00
{
return new TestLog(fullName, config.DebugEnabled, name);
2023-04-14 12:53:39 +00:00
}
protected override LogFile CreateLogFile()
{
return new LogFile(fullName, "log");
}
private string DetermineFolder(LogConfig config)
{
return Path.Join(
config.LogRoot,
$"{start.Year}-{Pad(start.Month)}",
Pad(start.Day));
}
private string GetFixtureName(string name)
2023-04-14 12:53:39 +00:00
{
var test = TestContext.CurrentContext.Test;
var className = test.ClassName!.Substring(test.ClassName.LastIndexOf('.') + 1);
if (!string.IsNullOrEmpty(name)) className = name;
2023-04-14 12:53:39 +00:00
return $"{Pad(start.Hour)}-{Pad(start.Minute)}-{Pad(start.Second)}Z_{className.Replace('.', '-')}";
}
private static string Pad(int n)
{
return n.ToString().PadLeft(2, '0');
}
}
}