Упрощён клиент

This commit is contained in:
2026-06-04 13:28:22 +03:00
parent 1b85ec5ce8
commit 6e6831c232
11 changed files with 273 additions and 306 deletions

View File

@@ -1,52 +1,57 @@
namespace ConsoleApp.Logging;
internal sealed class TeeTextWriter(TextWriter consoleWriter, TextWriter logWriter) : TextWriter
public sealed class ConsoleLog : IDisposable
{
public override System.Text.Encoding Encoding => consoleWriter.Encoding;
private readonly StreamWriter _file;
public override void Write(char value)
private ConsoleLog(string filePath, DateTime startedAt)
{
consoleWriter.Write(value);
logWriter.Write(value);
FilePath = filePath;
StartedAt = startedAt;
_file = new StreamWriter(filePath, append: true) { AutoFlush = true };
_file.WriteLine($"Запуск: {startedAt:yyyy-MM-dd HH:mm:ss}");
}
public override void Write(string? value)
public string FilePath { get; }
public DateTime StartedAt { get; }
public static ConsoleLog Open(string? fileName = null)
{
consoleWriter.Write(value);
logWriter.Write(value);
var startedAt = TruncateToSeconds(DateTime.Now);
fileName ??= $"test-sms-console-app-{startedAt:yyyyMMdd_HHmmss}.log";
return new ConsoleLog(Path.GetFullPath(fileName), startedAt);
}
public override void WriteLine(string? value)
private static DateTime TruncateToSeconds(DateTime value) =>
new(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Kind);
public void Write(string? text)
{
consoleWriter.WriteLine(value);
logWriter.WriteLine(value);
Console.Write(text);
_file.Write(text);
}
public override void Flush()
public void WriteLine(string? text = null)
{
consoleWriter.Flush();
logWriter.Flush();
}
protected override void Dispose(bool disposing)
{
if (disposing)
if (text is null)
{
logWriter.Dispose();
Console.WriteLine();
_file.WriteLine();
return;
}
base.Dispose(disposing);
Console.WriteLine(text);
_file.WriteLine(text);
}
}
internal static class ConsoleLog
{
public static IDisposable Configure()
public string? ReadLine(string prompt)
{
var logPath = $"test-sms-console-app-{DateTime.Now:yyyyMMdd}.log";
var logWriter = new StreamWriter(logPath, append: true) { AutoFlush = true };
var tee = new TeeTextWriter(global::System.Console.Out, logWriter);
global::System.Console.SetOut(tee);
return logWriter;
Write(prompt);
var line = Console.ReadLine();
_file.WriteLine(line ?? "");
return line;
}
public void Dispose() => _file.Dispose();
}