From ffe61a7cf745230b1436dbedf1610af72a618a0c Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Thu, 15 Mar 2018 16:12:43 +0200 Subject: Working on machine events. --- .../EventLogging/DefaultEventLogger.cs | 153 +++++++++++++++++---- 1 file changed, 126 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 c22ce335c..ff4badffe 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs @@ -1,4 +1,5 @@ -using System; +using Google.Protobuf; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -8,21 +9,32 @@ using System.Threading.Tasks; using Tango.BL; using Tango.BL.Entities; using Tango.BL.Enumerations; +using Tango.Core; +using Tango.Integration.Services; using Tango.Logging; using Tango.MachineStudio.Common.Authentication; +using Tango.MachineStudio.Common.Diagnostics; using Tango.MachineStudio.Common.StudioApplication; using Tango.PMR.Diagnostics; +using Tango.Integration.Operation; namespace Tango.MachineStudio.Common.EventLogging { - public class DefaultEventLogger : IEventLogger + /// + /// Represents the default database events logger. + /// + /// + public class DefaultEventLogger : ExtendedObject, IEventLogger { private ObservablesContext _db; private Thread _logThread; private ConcurrentQueue _events; private IStudioApplicationManager _application; private IAuthenticationProvider _authentication; - private Dictionary _eventTypesGuids; + private Dictionary _eventTypesGuids; + private String _hostName; + + #region Constructors /// /// Initializes a new instance of the class. @@ -31,12 +43,14 @@ namespace Tango.MachineStudio.Common.EventLogging /// The authentication provider. public DefaultEventLogger(IStudioApplicationManager applicationManager, IAuthenticationProvider authenticationProvider) { + _hostName = Environment.MachineName; + _events = new ConcurrentQueue(); _db = ObservablesContext.CreateDefault(); _db.Configuration.LazyLoadingEnabled = false; - _eventTypesGuids = new Dictionary(); + _eventTypesGuids = new Dictionary(); _db.ActionTypes.ToList(); _db.EventTypesCategories.ToList(); @@ -45,7 +59,7 @@ namespace Tango.MachineStudio.Common.EventLogging foreach (var type in _db.EventTypes) { - _eventTypesGuids.Add((EventTypes)type.Code, type.Guid); + _eventTypesGuids.Add((EventTypes)type.Code, type); } _application = applicationManager; @@ -53,14 +67,93 @@ namespace Tango.MachineStudio.Common.EventLogging _logThread = new Thread(LogThreadMethod); _logThread.IsBackground = true; _logThread.Start(); + + _application.ConnectedMachineChanged += _application_ConnectedMachineChanged; + } + + #endregion + + #region Event Handlers + + /// + /// Handle the application manager connected machine changed event. + /// + /// The sender. + /// The machine. + private void _application_ConnectedMachineChanged(object sender, IExternalBridgeClient machine) + { + if (machine != null) + { + if (machine.MachineEventsStateProvider != null) + { + machine.MachineEventsStateProvider.NewEvents -= MachineEventsStateProvider_NewEvents; + machine.MachineEventsStateProvider.NewEvents += MachineEventsStateProvider_NewEvents; + } + + machine.RequestSent -= Machine_RequestSent; + machine.RequestFailed -= Machine_RequestFailed; + machine.ResponseReceived -= Machine_ResponseReceived; + + machine.RequestSent += Machine_RequestSent; + machine.RequestFailed += Machine_RequestFailed; + machine.ResponseReceived += Machine_ResponseReceived; + } + } + + /// + /// Handles the RequestSent event of the connected machine. + /// + /// The sender. + /// The message. + private void Machine_RequestSent(object sender, IMessage message) + { + Log(EventTypes.RequestSent, String.Format("Sending request '{0}'...{1}{2}", message.GetType().Name, Environment.NewLine, message.ToJsonString())); } + /// + /// Handles the RequestFailed event of the connected machine. + /// + /// The source of the event. + /// The instance containing the event data. + private void Machine_RequestFailed(object sender, RequestFailedEventArgs e) + { + Log(EventTypes.RequestFailed, String.Format("Request failed '{0}'...{1}{2}{1}{3}", e.Message.GetType().Name, Environment.NewLine, e.Message.ToJsonString(), e.Exception.ToString())); + } + + /// + /// Handles the ResponseReceived event of the connected machine. + /// + /// The sender. + /// The message. + private void Machine_ResponseReceived(object sender, IMessage message) + { + Log(EventTypes.ResponseReceived, String.Format("Response received '{0}'...{1}{2}", message.GetType().Name, Environment.NewLine, message.ToJsonString())); + } + + /// + /// Handles the connected machine events state provider NewEvents event. + /// + /// The sender. + /// The events. + private void MachineEventsStateProvider_NewEvents(object sender, IEnumerable events) + { + foreach (var ev in events) + { + Log(ev); + } + } + + #endregion + + #region Logging + /// /// Logs the specified machine event. /// /// The machine event. public void Log(MachinesEvent machineEvent) { + LogManager.Log("Logging event " + machineEvent.EventType.Name + " - " + machineEvent.Description); _events.Enqueue(machineEvent); } @@ -72,11 +165,12 @@ namespace Tango.MachineStudio.Common.EventLogging public void Log(EventTypes eventType, string message) { MachinesEvent machineEvent = new MachinesEvent(); - machineEvent.MachineGuid = _application.ConnectedMachine.Guid; + machineEvent.MachineGuid = _application.ConnectedMachine != null ? _application.ConnectedMachine.Guid : null; machineEvent.DateTime = DateTime.UtcNow; machineEvent.Description = message; - machineEvent.EventTypeGuid = _eventTypesGuids[eventType]; - machineEvent.UserGuid = _authentication.CurrentUser.Guid; + machineEvent.EventType = _eventTypesGuids[eventType]; + machineEvent.UserGuid = _authentication.CurrentUser != null ? _authentication.CurrentUser.Guid : null; + machineEvent.HostName = _hostName; Log(machineEvent); } @@ -96,7 +190,7 @@ namespace Tango.MachineStudio.Common.EventLogging /// The exception. public void Log(Exception exception) { - Log(EventTypes.ApplicationException, exception.Message); + Log(EventTypes.ApplicationException, exception.ToString()); } /// @@ -108,37 +202,42 @@ namespace Tango.MachineStudio.Common.EventLogging Log(EventTypes.ApplicationInformation, message); } + /// + /// Logging thread loop. + /// private void LogThreadMethod() { - try + while (true) { - while (true) + bool _saveChanges = false; + + while (_events.Count > 0) { - bool _saveChanges = false; + MachinesEvent ev = null; - while (_events.Count > 0) + if (_events.TryDequeue(out ev)) { - MachinesEvent ev = null; - - if (_events.TryDequeue(out ev)) - { - _db.MachinesEvents.Add(ev); - _saveChanges = true; - } + _db.MachinesEvents.Add(ev); + _saveChanges = true; } + } - if (_saveChanges) + if (_saveChanges) + { + try { _db.SaveChanges(); } - - Thread.Sleep(5000); + catch (Exception ex) + { + LogManager.Log(ex, "Error saving machine event to database."); + } } - } - catch (Exception ex) - { - LogManager.Default.Log(ex, "Error saving machine event to database."); + + Thread.Sleep(5000); } } + + #endregion } } -- cgit v1.3.1