diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs | 153 |
1 files changed, 126 insertions, 27 deletions
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 + /// <summary> + /// Represents the default database events logger. + /// </summary> + /// <seealso cref="IEventLogger" /> + public class DefaultEventLogger : ExtendedObject, IEventLogger { private ObservablesContext _db; private Thread _logThread; private ConcurrentQueue<MachinesEvent> _events; private IStudioApplicationManager _application; private IAuthenticationProvider _authentication; - private Dictionary<EventTypes, String> _eventTypesGuids; + private Dictionary<EventTypes, BL.Entities.EventType> _eventTypesGuids; + private String _hostName; + + #region Constructors /// <summary> /// Initializes a new instance of the <see cref="DefaultEventLogger"/> class. @@ -31,12 +43,14 @@ namespace Tango.MachineStudio.Common.EventLogging /// <param name="authenticationProvider">The authentication provider.</param> public DefaultEventLogger(IStudioApplicationManager applicationManager, IAuthenticationProvider authenticationProvider) { + _hostName = Environment.MachineName; + _events = new ConcurrentQueue<MachinesEvent>(); _db = ObservablesContext.CreateDefault(); _db.Configuration.LazyLoadingEnabled = false; - _eventTypesGuids = new Dictionary<EventTypes, string>(); + _eventTypesGuids = new Dictionary<EventTypes, BL.Entities.EventType>(); _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 + + /// <summary> + /// Handle the application manager connected machine changed event. + /// </summary> + /// <param name="sender">The sender.</param> + /// <param name="machine">The machine.</param> + 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; + } + } + + /// <summary> + /// Handles the RequestSent event of the connected machine. + /// </summary> + /// <param name="sender">The sender.</param> + /// <param name="message">The message.</param> + 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())); } /// <summary> + /// Handles the RequestFailed event of the connected machine. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="RequestFailedEventArgs"/> instance containing the event data.</param> + 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())); + } + + /// <summary> + /// Handles the ResponseReceived event of the connected machine. + /// </summary> + /// <param name="sender">The sender.</param> + /// <param name="message">The message.</param> + 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())); + } + + /// <summary> + /// Handles the connected machine events state provider NewEvents event. + /// </summary> + /// <param name="sender">The sender.</param> + /// <param name="events">The events.</param> + private void MachineEventsStateProvider_NewEvents(object sender, IEnumerable<Event> events) + { + foreach (var ev in events) + { + Log(ev); + } + } + + #endregion + + #region Logging + + /// <summary> /// Logs the specified machine event. /// </summary> /// <param name="machineEvent">The machine event.</param> 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 /// <param name="exception">The exception.</param> public void Log(Exception exception) { - Log(EventTypes.ApplicationException, exception.Message); + Log(EventTypes.ApplicationException, exception.ToString()); } /// <summary> @@ -108,37 +202,42 @@ namespace Tango.MachineStudio.Common.EventLogging Log(EventTypes.ApplicationInformation, message); } + /// <summary> + /// Logging thread loop. + /// </summary> 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 } } |
