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 +++++++++++++++++++++ .../EventLogging/IEventLogger.cs | 45 +++++++ .../Tango.MachineStudio.Common.csproj | 9 ++ .../Tango.MachineStudio.Common/app.config | 10 ++ .../Tango.MachineStudio.Common/packages.config | 1 + 5 files changed, 209 insertions(+) create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs (limited to 'Software/Visual_Studio/MachineStudio') 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."); + } + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs new file mode 100644 index 000000000..645bd4a95 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/IEventLogger.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.BL.Enumerations; +using Tango.PMR.Diagnostics; + +namespace Tango.MachineStudio.Common.EventLogging +{ + public interface IEventLogger + { + /// + /// Logs the specified machine event. + /// + /// The machine event. + void Log(MachinesEvent machineEvent); + + /// + /// Logs the specified event type. + /// + /// Type of the event. + /// The message. + void Log(EventTypes eventType, String message); + + /// + /// Logs the specified hardware event. + /// + /// The hardware event. + void Log(Event hardwareEvent); + + /// + /// Logs the specified exception using the . + /// + /// The exception. + void Log(Exception exception); + + /// + /// Logs the specified message using the . + /// + /// The message. + void Log(String message); + } +} 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 6dfc72c7f..529419bd7 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 @@ -31,6 +31,12 @@ 4 + + ..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll + + + ..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll + ..\..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.dll @@ -53,6 +59,7 @@ ..\..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll + @@ -90,6 +97,8 @@ + + diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/app.config b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/app.config index cacd4cd77..4a6cb0526 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/app.config +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/app.config @@ -1,5 +1,9 @@  + + +
+ @@ -8,4 +12,10 @@ + + + + + + \ No newline at end of file diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/packages.config b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/packages.config index 4fd672b32..cf0df03c8 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/packages.config +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/packages.config @@ -1,6 +1,7 @@  + -- cgit v1.3.1