40 lines
1.3 KiB
C#
40 lines
1.3 KiB
C#
using System.Globalization;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier;
|
|
|
|
/// <summary>
|
|
/// Best-effort diagnostics: writes UTC-timestamped lines to stderr and, when a log path is configured,
|
|
/// appends them to a file (relative to the exe). Never throws — diagnostics must not break the YES/NO path.
|
|
/// </summary>
|
|
internal sealed class DiagLog(string? logPath)
|
|
{
|
|
private readonly string? _logPath = string.IsNullOrWhiteSpace(logPath) ? null : logPath;
|
|
|
|
public void Write(string message)
|
|
{
|
|
var line = string.Create(CultureInfo.InvariantCulture, $"{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ} {message}");
|
|
Console.Error.WriteLine(line);
|
|
|
|
if (_logPath is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var fullPath = Path.IsPathRooted(_logPath) ? _logPath : Path.Combine(AppContext.BaseDirectory, _logPath);
|
|
var dir = Path.GetDirectoryName(fullPath);
|
|
if (!string.IsNullOrEmpty(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
|
|
File.AppendAllText(fullPath, line + Environment.NewLine);
|
|
}
|
|
catch
|
|
{
|
|
// diagnostics are best-effort; swallow file errors so the YES/NO contract is unaffected
|
|
}
|
|
}
|
|
}
|