2
0
mirror of synced 2025-01-17 03:51:20 +00:00

112 lines
3.0 KiB
C#
Raw Normal View History

using CodexDistTestCore.Config;
using NUnit.Framework;
2023-03-20 11:37:02 +01:00
2023-03-21 13:20:21 +01:00
namespace CodexDistTestCore
2023-03-20 11:37:02 +01:00
{
public class TestLog
{
2023-03-26 10:08:10 +02:00
private readonly NumberSource subfileNumberSource = new NumberSource(0);
2023-03-22 09:22:18 +01:00
private readonly LogFile file;
2023-03-26 10:08:10 +02:00
private readonly DateTime now;
2023-03-20 11:37:02 +01:00
2023-03-22 09:22:18 +01:00
public TestLog()
2023-03-20 11:37:02 +01:00
{
2023-03-26 10:08:10 +02:00
now = DateTime.UtcNow;
2023-03-20 11:37:02 +01:00
var name = GetTestName();
2023-03-26 10:08:10 +02:00
file = new LogFile(now, name);
2023-03-20 11:37:02 +01:00
Log($"Begin: {name}");
}
2023-03-22 09:22:18 +01:00
public void Log(string message)
2023-03-20 11:37:02 +01:00
{
2023-03-22 09:22:18 +01:00
file.Write(message);
}
2023-03-20 11:37:02 +01:00
2023-03-22 09:22:18 +01:00
public void Error(string message)
{
Log($"[ERROR] {message}");
}
2023-03-21 13:20:21 +01:00
public void EndTest()
2023-03-22 09:22:18 +01:00
{
2023-03-20 11:37:02 +01:00
var result = TestContext.CurrentContext.Result;
Log($"Finished: {GetTestName()} = {result.Outcome.Status}");
2023-03-22 09:50:24 +01:00
if (!string.IsNullOrEmpty(result.Message))
{
Log(result.Message);
Log($"{result.StackTrace}");
2023-03-20 11:37:02 +01:00
}
}
2023-03-26 10:08:10 +02:00
public LogFile CreateSubfile()
{
return new LogFile(now, $"{GetTestName()}_{subfileNumberSource.GetNextNumber().ToString().PadLeft(6, '0')}");
}
2023-03-20 11:37:02 +01:00
private static string GetTestName()
{
var test = TestContext.CurrentContext.Test;
var className = test.ClassName!.Substring(test.ClassName.LastIndexOf('.') + 1);
2023-03-22 10:38:10 +01:00
var args = FormatArguments(test);
return $"{className}.{test.MethodName}{args}";
}
private static string FormatArguments(TestContext.TestAdapter test)
{
if (test.Arguments == null || !test.Arguments.Any()) return "";
return $"[{string.Join(',', test.Arguments)}]";
2023-03-20 11:37:02 +01:00
}
2023-03-21 16:09:41 +01:00
}
2023-03-20 11:37:02 +01:00
public class LogFile
{
2023-03-20 15:42:42 +01:00
private readonly string filepath;
2023-03-20 11:37:02 +01:00
2023-03-26 10:08:10 +02:00
public LogFile(DateTime now, string name)
2023-03-20 11:37:02 +01:00
{
2023-03-20 15:42:42 +01:00
filepath = Path.Join(
LogConfig.LogRoot,
2023-03-20 11:37:02 +01:00
$"{now.Year}-{Pad(now.Month)}",
Pad(now.Day));
Directory.CreateDirectory(filepath);
2023-03-26 10:08:10 +02:00
FilenameWithoutPath = $"{Pad(now.Hour)}-{Pad(now.Minute)}-{Pad(now.Second)}Z_{name.Replace('.', '-')}.log";
FullFilename = Path.Combine(filepath, FilenameWithoutPath);
2023-03-20 11:37:02 +01:00
}
public string FullFilename { get; }
2023-03-26 10:08:10 +02:00
public string FilenameWithoutPath { get; }
2023-03-20 11:37:02 +01:00
public void Write(string message)
{
WriteRaw($"{GetTimestamp()} {message}");
}
2023-03-26 10:08:10 +02:00
public void WriteRaw(string message)
2023-03-20 11:37:02 +01:00
{
try
{
File.AppendAllLines(FullFilename, new[] { message });
2023-03-20 11:37:02 +01:00
}
catch (Exception ex)
{
Console.WriteLine("Writing to log has failed: " + ex);
}
}
private static string Pad(int n)
{
return n.ToString().PadLeft(2, '0');
}
private static string GetTimestamp()
{
return $"[{DateTime.UtcNow.ToString("u")}]";
}
}
}