From d87881183d74fa692b598f170dd820adeaf54fca Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 14 Mar 2018 19:02:38 +0200 Subject: Working on event handling ! --- .../EventLogging/DefaultEventLogger.cs | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs (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 new file mode 100644 index 000000000..c22ce335c --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Entities; +using Tango.BL.Enumerations; +using Tango.Logging; +using Tango.MachineStudio.Common.Authentication; +using Tango.MachineStudio.Common.StudioApplication; +using Tango.PMR.Diagnostics; + +namespace Tango.MachineStudio.Common.EventLogging +{ + public class DefaultEventLogger : IEventLogger + { + private ObservablesContext _db; + private Thread _logThread; + private ConcurrentQueue _events; + private IStudioApplicationManager _application; + private IAuthenticationProvider _authentication; + private Dictionary _eventTypesGuids; + + /// + /// Initializes a new instance of the class. + /// + /// The application manager. + /// The authentication provider. + public DefaultEventLogger(IStudioApplicationManager applicationManager, IAuthenticationProvider authenticationProvider) + { + _events = new ConcurrentQueue(); + + _db = ObservablesContext.CreateDefault(); + _db.Configuration.LazyLoadingEnabled = false; + + _eventTypesGuids = new Dictionary(); + + _db.ActionTypes.ToList(); + _db.EventTypesCategories.ToList(); + _db.EventTypesGroups.ToList(); + _db.EventTypes.ToList(); + + foreach (var type in _db.EventTypes) + { + _eventTypesGuids.Add((EventTypes)type.Code, type.Guid); + } + + _application = applicationManager; + _authentication = authenticationProvider; + _logThread = new Thread(LogThreadMethod); + _logThread.IsBackground = true; + _logThread.Start(); + } + + /// + /// Logs the specified machine event. + /// + /// The machine event. + public void Log(MachinesEvent machineEvent) + { + _events.Enqueue(machineEvent); + } + + /// + /// Logs the specified event type. + /// + /// Type of the event. + /// The message. + public void Log(EventTypes eventType, string message) + { + MachinesEvent machineEvent = new MachinesEvent(); + machineEvent.MachineGuid = _application.ConnectedMachine.Guid; + machineEvent.DateTime = DateTime.UtcNow; + machineEvent.Description = message; + machineEvent.EventTypeGuid = _eventTypesGuids[eventType]; + machineEvent.UserGuid = _authentication.CurrentUser.Guid; + + Log(machineEvent); + } + + /// + /// Logs the specified hardware event. + /// + /// The hardware event. + public void Log(Event hardwareEvent) + { + Log((EventTypes)hardwareEvent.Type, hardwareEvent.Message); + } + + /// + /// Logs the specified exception using the . + /// + /// The exception. + public void Log(Exception exception) + { + Log(EventTypes.ApplicationException, exception.Message); + } + + /// + /// Logs the specified message using the . + /// + /// The message. + public void Log(String message) + { + Log(EventTypes.ApplicationInformation, message); + } + + private void LogThreadMethod() + { + try + { + while (true) + { + bool _saveChanges = false; + + while (_events.Count > 0) + { + MachinesEvent ev = null; + + if (_events.TryDequeue(out ev)) + { + _db.MachinesEvents.Add(ev); + _saveChanges = true; + } + } + + if (_saveChanges) + { + _db.SaveChanges(); + } + + Thread.Sleep(5000); + } + } + catch (Exception ex) + { + LogManager.Default.Log(ex, "Error saving machine event to database."); + } + } + } +} -- cgit v1.3.1