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

60 lines
1.9 KiB
C#
Raw Normal View History

2023-07-18 07:02:41 +00:00
using Newtonsoft.Json;
namespace Logging
{
public class StatusLog
{
private readonly object fileLock = new object();
private readonly string fullName;
private readonly string fixtureName;
private readonly string codexId;
public StatusLog(LogConfig config, DateTime start, string codexId, string name = "")
{
fullName = NameUtils.GetFixtureFullName(config, start, name) + "_STATUS.log";
2023-07-18 07:02:41 +00:00
fixtureName = NameUtils.GetRawFixtureName();
this.codexId = codexId;
}
2023-07-18 07:47:44 +00:00
public void ConcludeTest(string resultStatus)
2023-07-18 07:02:41 +00:00
{
Write(new StatusLogJson
{
time = DateTime.UtcNow.ToString("u"),
2023-07-18 09:04:14 +00:00
runid = NameUtils.GetRunId(),
2023-07-18 07:47:44 +00:00
status = resultStatus,
2023-07-18 09:04:14 +00:00
testid = NameUtils.GetTestId(),
2023-07-18 07:02:41 +00:00
codexid = codexId,
fixturename = fixtureName,
2023-07-18 09:04:14 +00:00
testname = NameUtils.GetTestMethodName()
2023-07-18 07:02:41 +00:00
});
}
private void Write(StatusLogJson json)
{
try
{
lock (fileLock)
{
File.AppendAllLines(fullName, new[] { JsonConvert.SerializeObject(json) });
}
}
catch (Exception ex)
{
Console.WriteLine("Unable to write to status log: " + ex);
}
}
}
public class StatusLogJson
{
public string time { get; set; } = string.Empty;
public string runid { get; set; } = string.Empty;
public string status { get; set; } = string.Empty;
public string testid { get; set; } = string.Empty;
public string codexid { get; set; } = string.Empty;
public string fixturename { get; set; } = string.Empty;
public string testname { get; set; } = string.Empty;
}
}