using System; using System.Threading; using System.Windows.Forms; using System.Drawing; namespace _Log { public class Log { TextBox tbLog; public Log(TextBox aTbLog) { tbLog = aTbLog; tbLog.WordWrap = false; tbLog.Multiline = true; tbLog.ScrollBars = ScrollBars.Both; tbLog.Font = new Font("Consolas", 14); } public void l(string s, string from = null) { string s2 = Thread.CurrentThread.IsBackground ? "background" : "UI thread"; s = $"{DateTime.Now.ToString("HH:mm:ss")}{" " + from} ({s2}) {s}\r\n"; if (!tbLog.InvokeRequired) tbLog.AppendText(s); else tbLog.Invoke(new Action(() => tbLog.AppendText(s))); } public static Log cli, srv; public Log(TextBox aTbLogSrv, TextBox aTbLogCli) { srv = new Log(aTbLogSrv); cli = new Log(aTbLogCli); } public static void S(string s) { srv?.l(s); // to jest równoważne: if (srv != null) srv.l(s); } public static void Clear() { srv?.tbLog.Clear(); cli?.tbLog.Clear(); } public static void C(string s) { cli?.l(s); } public static readonly string HR = $"\r\n{new string('—', 100)} "; public static void B(string s) { cli?.l(s); } public static void L(string s) { srv?.l(s); } } }