protected async virtual Task AddToDB(string txt) { Log Log = new Log() { Text = txt, AccountId = AccountId, Date = DateTime.Now }; OnLogAdded?.Invoke(txt); DB.Log.Add(Log); DB.SaveChanges(); }
public void Log(string message) { string date = DateTime.Now.ToString(); string msg = date + ": " + message; if (OnLogAdded != null) { OnLogAdded.Invoke(msg); } System.IO.File.AppendAllText(logFile, msg + "\r\n"); }
public Logger() { _log = new SynchronizedCollection <string>(); _tempQueue = new ConcurrentQueue <string>(); _tokenSource = new CancellationTokenSource(); _token = _tokenSource.Token; _eventTask = TaskFactory.StartNew(() => { var invokeEvent = new Action(() => { if (_tempQueue.IsEmpty) { _waitFlag = false; return; } var builder = new StringBuilder(); while (!_tempQueue.IsEmpty) { string str; _tempQueue.TryDequeue(out str); builder.Append(str); } OnLogAdded?.Invoke(builder.ToString()); if (_waitFlag) { _waitFlag = false; } }); while (true) { Thread.Sleep(500); invokeEvent.Invoke(); if (_token.IsCancellationRequested) { invokeEvent.Invoke(); return; } } }); }