Writes pod logs to separate files.

This commit is contained in:
benbierens 2023-03-20 15:42:42 +01:00
parent 0e2bd7f897
commit d0faf79f6c
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
2 changed files with 14 additions and 15 deletions

View File

@ -72,7 +72,7 @@ namespace CodexDistTests.TestCore
WaitUntilNamespaceDeleted(client);
}
public void FetchAllPodsLogs(Action<string, Stream> onLog)
public void FetchAllPodsLogs(Action<string, string, Stream> onLog)
{
var client = CreateClient();
foreach (var node in activeNodes.Values)
@ -81,7 +81,7 @@ namespace CodexDistTests.TestCore
foreach (var podName in node.ActivePodNames)
{
var stream = client.ReadNamespacedPodLog(podName, k8sNamespace);
onLog($"{nodeDescription}:{podName}", stream);
onLog(node.SelectorName, $"{nodeDescription}:{podName}", stream);
}
}
}

View File

@ -41,7 +41,6 @@ namespace CodexDistTests.TestCore
IncludeFullPodLogging(k8sManager);
}
LogRaw("");
file = null;
}
@ -52,26 +51,26 @@ namespace CodexDistTests.TestCore
return $"{className}.{test.MethodName}";
}
private static void LogRaw(string message)
private static void LogRaw(string message, string filename)
{
file!.WriteRaw(message);
file!.WriteRaw(message, filename);
}
private static void IncludeFullPodLogging(K8sManager k8sManager)
{
LogRaw("Full pod logging:");
Log("Full pod logging:");
k8sManager.FetchAllPodsLogs(WritePodLog);
}
private static void WritePodLog(string nodeDescription, Stream stream)
private static void WritePodLog(string id, string nodeDescription, Stream stream)
{
LogRaw("---");
LogRaw(nodeDescription);
Log($"{nodeDescription} -->> {id}");
LogRaw(nodeDescription, id);
var reader = new StreamReader(stream);
var line = reader.ReadLine();
while (line != null)
{
LogRaw(line);
LogRaw(line, id);
line = reader.ReadLine();
}
}
@ -79,21 +78,21 @@ namespace CodexDistTests.TestCore
public class LogFile
{
private readonly string filepath;
private readonly string filename;
public LogFile(string name)
{
var now = DateTime.UtcNow;
var filepath = Path.Join(
filepath = Path.Join(
TestLog.LogRoot,
$"{now.Year}-{Pad(now.Month)}",
Pad(now.Day));
Directory.CreateDirectory(filepath);
filename = Path.Combine(filepath,
$"{Pad(now.Hour)}-{Pad(now.Minute)}-{Pad(now.Second)}Z_{name.Replace('.', '-')}.log");
filename = Path.Combine(filepath, $"{Pad(now.Hour)}-{Pad(now.Minute)}-{Pad(now.Second)}Z_{name.Replace('.', '-')}");
}
public void Write(string message)
@ -101,11 +100,11 @@ namespace CodexDistTests.TestCore
WriteRaw($"{GetTimestamp()} {message}");
}
public void WriteRaw(string message)
public void WriteRaw(string message, string subfile = "")
{
try
{
File.AppendAllLines(filename, new[] { message });
File.AppendAllLines(filename + subfile + ".log", new[] { message });
}
catch (Exception ex)
{