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 +++++++++++++++------- .../EventLogging/IEventLogger.cs | 11 +++ .../Messages/MachineConnectionChangedMessage.cs | 14 ++++ .../Tango.MachineStudio.Common.csproj | 1 + 4 files changed, 91 insertions(+), 27 deletions(-) create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Messages/MachineConnectionChangedMessage.cs (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common') 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."); + } } } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs index aeecb4ae6..ce599a398 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs @@ -44,10 +44,21 @@ namespace Tango.MachineStudio.Common.EventLogging /// The exception. void Log(Exception exception); + /// + /// Logs the specified exception using the . + /// + /// The exception. + void Log(Exception exception, String description); + /// /// Logs the specified message using the . /// /// The message. void Log(String message); + + /// + /// Immediately saves all pending events to database. + /// + void FlushAll(); } } diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Messages/MachineConnectionChangedMessage.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Messages/MachineConnectionChangedMessage.cs new file mode 100644 index 000000000..90820ed47 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Messages/MachineConnectionChangedMessage.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Integration.Services; + +namespace Tango.MachineStudio.Common.Messages +{ + public class MachineConnectionChangedMessage : IStudioMessage + { + public IExternalBridgeClient Machine { get; set; } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj index 15ab97a27..df553f502 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj @@ -105,6 +105,7 @@ + -- cgit v1.3.1