cs-codex-dist-tests/Tests/DistTestCore/Logs/StatusLog.cs

59 lines
2.1 KiB
C#
Raw Normal View History

2023-09-13 14:06:05 +00:00
using Logging;
using Newtonsoft.Json;
using System.Globalization;
2023-09-13 14:06:05 +00:00
2023-09-20 08:51:47 +00:00
namespace DistTestCore.Logs
2023-09-13 14:06:05 +00:00
{
public class StatusLog
{
private readonly object fileLock = new();
private readonly string deployId;
2023-09-13 14:06:05 +00:00
private readonly string fullName;
private readonly string fixtureName;
2023-11-10 14:28:53 +00:00
private readonly string testType;
2023-09-13 14:06:05 +00:00
public StatusLog(LogConfig config, DateTime start, string testType, string deployId, string name = "")
2023-09-13 14:06:05 +00:00
{
fullName = NameUtils.GetFixtureFullName(config, start, name) + "_STATUS.log";
fixtureName = NameUtils.GetRawFixtureName();
2023-11-10 14:28:53 +00:00
this.testType = testType;
this.deployId = deployId;
2023-09-13 14:06:05 +00:00
}
public void ConcludeTest(string resultStatus, TimeSpan testDuration, Dictionary<string, string> data)
{
ConcludeTest(resultStatus, testDuration.TotalSeconds.ToString(CultureInfo.InvariantCulture), data);
}
2023-09-13 14:06:05 +00:00
public void ConcludeTest(string resultStatus, string testDuration, Dictionary<string, string> data)
{
data.Add("timestamp", DateTime.UtcNow.ToString("o"));
data.Add("deployid", deployId);
2023-09-13 14:06:05 +00:00
data.Add("status", resultStatus);
data.Add("category", NameUtils.GetCategoryName());
data.Add("fixturename", fixtureName);
if (!data.ContainsKey("testname")) data.Add("testname", NameUtils.GetTestMethodName());
2023-11-10 14:28:53 +00:00
data.Add("testid", NameUtils.GetTestId());
data.Add("testtype", testType);
2023-09-13 14:06:05 +00:00
data.Add("testduration", testDuration);
data.Add("testframeworkrevision", GitInfo.GetStatus());
2023-09-13 14:06:05 +00:00
Write(data);
}
private void Write(Dictionary<string, string> data)
{
try
{
lock (fileLock)
{
File.AppendAllLines(fullName, new[] { JsonConvert.SerializeObject(data) });
}
}
catch (Exception ex)
{
Console.WriteLine("Unable to write to status log: " + ex);
}
}
}
}