L1a: Core und Module von WinForms entkoppeln - net10.0 statt net10.0-windows
Core, alle drei Module und das Testprojekt tragen keinen UI-Code mehr und bauen fuer linux-x64. Nur noch IBKRTrader.App ist Windows-gebunden. UI-Contract toolkit-neutral (Vorbild: PolytraderSharp): - ModuleView.CreateForm (Func<Form>) -> CreateView (Func<object>) - ModuleView.Icon (System.Drawing.Image) -> IconKey (string). System.Drawing.Common ist seit .NET 7 Windows-only und wirft auf Linux. - WindowMenu.cs war reine WinForms-Umsetzung -> in die Shell verschoben. LoggingService haelt keine RichTextBox mehr, sondern meldet Eintraege ueber event EntryWritten. Einfaerbung und UI-Thread-Wechsel liegen jetzt im LogPanelController der Shell. Nebenbei: ToUpper() -> ToUpperInvariant() (tr-TR haette aus "info" ein "İNFO" gemacht) und \r\n -> Environment.NewLine. Die drei Modul-Fenster liegen jetzt unter UI/Views/Modules/; RegisterUi der Module ist bewusst leer, die Shell registriert sie zentral ueber UI/ModuleViews.cs (nur fuer tatsaechlich geladene Module). ViewIcons loest IconKey gegen die PNG-Ressourcen auf - dieselben Schluessel bekommt spaeter die Avalonia-Shell. UiConstructionTests entfernt: die Konstruktionspruefung deckt --smoke-ui ab, das Testprojekt braucht dafuer keine UI-Referenz mehr. Der Test RegisterUi_RegistersMainView haelt jetzt das Gegenteil fest - das Modul darf nichts registrieren, sonst waere es wieder toolkit-gebunden. Verifiziert: Build 0 Fehler/0 Warnungen, 163 Tests gruen, --smoke-ui konstruiert alle 7 Fenster, und Core + 3 Module + Tests bauen fuer linux-x64. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,22 +4,32 @@ namespace IBKRTrader.Core.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// Thread-sicherer Logging-Service.
|
||||
/// – Schreibt farbig in die RichTextBox (UI-Thread-safe via BeginInvoke)
|
||||
/// – Schreibt in Logs\{Module}\{Level}-dd-MM-yy.txt
|
||||
/// – Schreibt in Logs\{Module}\{Level}-dd-MM-yy.txt sowie strukturiert nach Logs\{Datum}.jsonl
|
||||
/// – Meldet jeden Eintrag über <see cref="EntryWritten"/> an interessierte Senken (z. B. die
|
||||
/// Live-Log-Ansicht der Oberfläche)
|
||||
///
|
||||
/// <para><b>Bewusst ohne UI-Bezug:</b> Früher hielt dieser Dienst direkt eine
|
||||
/// <c>RichTextBox</c> samt <c>System.Drawing.Color</c> und marshallte selbst auf den UI-Thread.
|
||||
/// Damit hing der Core an WinForms. Jetzt kennt er nur noch das Ereignis; Einfärbung und
|
||||
/// Thread-Wechsel sind Sache der jeweiligen Oberfläche.</para>
|
||||
/// </summary>
|
||||
public class LoggingService
|
||||
{
|
||||
private RichTextBox? _rtb;
|
||||
private AppLogLevel _minLevel = AppLogLevel.Info;
|
||||
private readonly object _fileLock = new();
|
||||
private readonly object _jsonlLock = new();
|
||||
|
||||
private static readonly string LogBaseDir =
|
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
|
||||
Path.Combine(AppContext.BaseDirectory, "Logs");
|
||||
|
||||
// ─── Konfiguration ────────────────────────────────────────────────────────
|
||||
|
||||
public void AttachRichTextBox(RichTextBox rtb) => _rtb = rtb;
|
||||
/// <summary>
|
||||
/// Feuert für jeden geschriebenen Eintrag (nach der Mindest-Level-Prüfung). Die Oberfläche
|
||||
/// hängt sich hier ein; das Marshalling auf den UI-Thread übernimmt sie selbst, weil dieser
|
||||
/// Dienst aus beliebigen Worker-Threads schreibt.
|
||||
/// </summary>
|
||||
public event Action<LogEntry>? EntryWritten;
|
||||
|
||||
public void SetMinLevel(AppLogLevel level) => _minLevel = level;
|
||||
|
||||
@@ -51,7 +61,13 @@ public class LoggingService
|
||||
var entry = new LogEntry(DateTime.Now, level, module, message, ex);
|
||||
WriteToFile(entry);
|
||||
WriteToJsonl(entry, cid);
|
||||
WriteToRtb(entry);
|
||||
NotifySinks(entry);
|
||||
}
|
||||
|
||||
private void NotifySinks(LogEntry e)
|
||||
{
|
||||
// Eine hängende Senke darf den schreibenden Worker nicht mitreißen.
|
||||
try { EntryWritten?.Invoke(e); } catch { /* Logging darf niemals abstürzen */ }
|
||||
}
|
||||
|
||||
// ─── Datei ────────────────────────────────────────────────────────────────
|
||||
@@ -66,10 +82,10 @@ public class LoggingService
|
||||
var file = Path.Combine(dir, $"{e.Level}-{e.Timestamp:dd-MM-yy}.txt");
|
||||
var line = $"[{e.Timestamp:HH:mm:ss}] {e.Message}";
|
||||
if (e.Exception != null)
|
||||
line += $"\r\n {e.Exception}";
|
||||
line += $"{Environment.NewLine} {e.Exception}";
|
||||
|
||||
lock (_fileLock)
|
||||
File.AppendAllText(file, line + "\r\n");
|
||||
File.AppendAllText(file, line + Environment.NewLine);
|
||||
}
|
||||
catch { /* Logging darf niemals abstürzen */ }
|
||||
}
|
||||
@@ -95,45 +111,18 @@ public class LoggingService
|
||||
catch { /* Logging darf niemals abstürzen */ }
|
||||
}
|
||||
|
||||
// ─── RichTextBox ──────────────────────────────────────────────────────────
|
||||
// ─── Anzeigeformat ────────────────────────────────────────────────────────
|
||||
|
||||
private static readonly Color ColorInfo = Color.FromArgb(150, 210, 150);
|
||||
private static readonly Color ColorWarn = Color.FromArgb(255, 190, 60);
|
||||
private static readonly Color ColorError = Color.FromArgb(255, 80, 80);
|
||||
|
||||
private void WriteToRtb(LogEntry e)
|
||||
/// <summary>
|
||||
/// Einzeilige Darstellung für Log-Ansichten. Liegt hier, damit jede Oberfläche dieselbe Zeile
|
||||
/// zeigt. <c>ToUpperInvariant</c> ist Absicht: <c>ToUpper()</c> würde unter tr-TR aus "info"
|
||||
/// ein "İNFO" machen.
|
||||
/// </summary>
|
||||
public static string Format(LogEntry e)
|
||||
{
|
||||
if (_rtb == null) return;
|
||||
try
|
||||
{
|
||||
var color = e.Level switch
|
||||
{
|
||||
AppLogLevel.Warn => ColorWarn,
|
||||
AppLogLevel.Error => ColorError,
|
||||
_ => ColorInfo
|
||||
};
|
||||
var text = $"[{e.Timestamp:HH:mm:ss}] [{e.Level.ToString().ToUpper(),-5}] [{e.Module}] {e.Message}";
|
||||
if (e.Exception != null)
|
||||
text += $"\r\n {e.Exception.Message}";
|
||||
text += "\r\n";
|
||||
|
||||
if (_rtb.InvokeRequired)
|
||||
_rtb.BeginInvoke(() => AppendColored(text, color));
|
||||
else
|
||||
AppendColored(text, color);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
private void AppendColored(string text, Color color)
|
||||
{
|
||||
if (_rtb == null) return;
|
||||
_rtb.SelectionStart = _rtb.TextLength;
|
||||
_rtb.SelectionLength = 0;
|
||||
_rtb.SelectionColor = color;
|
||||
_rtb.AppendText(text);
|
||||
_rtb.SelectionColor = _rtb.ForeColor;
|
||||
if (_rtb.TextLength > 0)
|
||||
_rtb.ScrollToCaret();
|
||||
var text = $"[{e.Timestamp:HH:mm:ss}] [{e.Level.ToString().ToUpperInvariant(),-5}] [{e.Module}] {e.Message}";
|
||||
if (e.Exception != null)
|
||||
text += $"{Environment.NewLine} {e.Exception.Message}";
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user