From 1061758f95b7ba633e6bcc2c3556b42f033b1a79 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Mon, 26 Mar 2018 14:21:53 +0300 Subject: Working on logging module ! Modified PID Control Table. --- .../EventLogging/DefaultEventLogger.cs | 92 +++++++++++++++------- 1 file changed, 65 insertions(+), 27 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs') diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs index 0f19d3b6d..d3fb0897f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs @@ -34,13 +34,14 @@ namespace Tango.MachineStudio.Common.EventLogging private Dictionary _eventTypesGuids; private String _hostName; private bool _isInitialized; + private List _pendingEvents; #region Events /// /// Occurs when a new machine event has been logged. /// - public event EventHandler NewLog; + public event EventHandler NewLog; #endregion @@ -56,6 +57,7 @@ namespace Tango.MachineStudio.Common.EventLogging _hostName = Environment.MachineName; _events = new ConcurrentQueue(); + _pendingEvents = new List(); _eventTypesGuids = new Dictionary(); @@ -90,7 +92,7 @@ namespace Tango.MachineStudio.Common.EventLogging _eventTypesGuids.Add((EventTypes)type.Code, type); } - _isInitialized = true; + _isInitialized = true; } } @@ -176,15 +178,33 @@ namespace Tango.MachineStudio.Common.EventLogging /// The machine event. public void Log(MachinesEvent machineEvent) { - machineEvent.MachineGuid = _application.ConnectedMachine != null ? _application.ConnectedMachine.Guid : null; - machineEvent.UserGuid = _authentication.CurrentUser != null ? _authentication.CurrentUser.Guid : null; machineEvent.HostName = _hostName; machineEvent.EventType = _eventTypesGuids[machineEvent.Type]; - LogManager.Log("Logging event " + machineEvent.EventType.Name + " - " + machineEvent.Description); - _events.Enqueue(machineEvent); + if (_application.ConnectedMachine == null || _authentication.CurrentUser == null) + { + _pendingEvents.Add(machineEvent); + } + else + { + if (_pendingEvents.Count > 0) + { + var pending = _pendingEvents.ToList(); + _pendingEvents.Clear(); + + foreach (var ev in pending) + { + Log(ev); + } + } - NewLog?.Invoke(this, machineEvent); + LogManager.Log("Logging event " + machineEvent.EventType.Name + " - " + machineEvent.Description); + machineEvent.MachineGuid = _application.ConnectedMachine.Guid; + machineEvent.UserGuid = _authentication.CurrentUser.Guid; + machineEvent.User = _authentication.CurrentUser; + _events.Enqueue(machineEvent); + NewLog?.Invoke(this, machineEvent); + } } /// @@ -222,6 +242,16 @@ namespace Tango.MachineStudio.Common.EventLogging Log(EventTypes.ApplicationException, exception.ToString()); } + /// + /// Logs the specified exception using the . + /// + /// The exception. + /// + public void Log(Exception exception, string description) + { + Log(EventTypes.ApplicationException, description + Environment.NewLine + exception.ToString()); + } + /// /// Logs the specified message using the . /// @@ -238,32 +268,40 @@ namespace Tango.MachineStudio.Common.EventLogging { while (true) { - bool _saveChanges = false; + FlushAll(); + Thread.Sleep(5000); + } + } - while (_events.Count > 0) - { - MachinesEvent ev = null; + /// + /// Immediately saves all pending events to database. + /// + public void FlushAll() + { + bool _saveChanges = false; - if (_events.TryDequeue(out ev)) - { - _db.MachinesEvents.Add(ev); - _saveChanges = true; - } - } + while (_events.Count > 0) + { + MachinesEvent ev = null; - if (_saveChanges) + if (_events.TryDequeue(out ev)) { - try - { - _db.SaveChanges(); - } - catch (Exception ex) - { - LogManager.Log(ex, "Error saving machine event to database."); - } + ev.User = null; + _db.MachinesEvents.Add(ev); + _saveChanges = true; } + } - Thread.Sleep(5000); + if (_saveChanges) + { + try + { + _db.SaveChanges(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error saving machine event to database."); + } } } -- cgit v1.3.1