aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-03-14 19:02:38 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-03-14 19:02:38 +0200
commitd87881183d74fa692b598f170dd820adeaf54fca (patch)
treed41721b5c5f9c7e8335bb5e65d7a16a20b4d901d /Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs
parent748d18fe3bfa60a586de151c3bb0d1fd608b1e13 (diff)
downloadTango-d87881183d74fa692b598f170dd820adeaf54fca.tar.gz
Tango-d87881183d74fa692b598f170dd820adeaf54fca.zip
Working on event handling !
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.cs144
1 files changed, 144 insertions, 0 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
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<MachinesEvent> _events;
+ private IStudioApplicationManager _application;
+ private IAuthenticationProvider _authentication;
+ private Dictionary<EventTypes, String> _eventTypesGuids;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="DefaultEventLogger"/> class.
+ /// </summary>
+ /// <param name="applicationManager">The application manager.</param>
+ /// <param name="authenticationProvider">The authentication provider.</param>
+ public DefaultEventLogger(IStudioApplicationManager applicationManager, IAuthenticationProvider authenticationProvider)
+ {
+ _events = new ConcurrentQueue<MachinesEvent>();
+
+ _db = ObservablesContext.CreateDefault();
+ _db.Configuration.LazyLoadingEnabled = false;
+
+ _eventTypesGuids = new Dictionary<EventTypes, string>();
+
+ _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();
+ }
+
+ /// <summary>
+ /// Logs the specified machine event.
+ /// </summary>
+ /// <param name="machineEvent">The machine event.</param>
+ public void Log(MachinesEvent machineEvent)
+ {
+ _events.Enqueue(machineEvent);
+ }
+
+ /// <summary>
+ /// Logs the specified event type.
+ /// </summary>
+ /// <param name="eventType">Type of the event.</param>
+ /// <param name="message">The message.</param>
+ 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);
+ }
+
+ /// <summary>
+ /// Logs the specified hardware event.
+ /// </summary>
+ /// <param name="hardwareEvent">The hardware event.</param>
+ public void Log(Event hardwareEvent)
+ {
+ Log((EventTypes)hardwareEvent.Type, hardwareEvent.Message);
+ }
+
+ /// <summary>
+ /// Logs the specified exception using the <see cref="EventTypes.ApplicationException"/>.
+ /// </summary>
+ /// <param name="exception">The exception.</param>
+ public void Log(Exception exception)
+ {
+ Log(EventTypes.ApplicationException, exception.Message);
+ }
+
+ /// <summary>
+ /// Logs the specified message using the <see cref="EventTypes.ApplicationInformation"/>.
+ /// </summary>
+ /// <param name="message">The message.</param>
+ 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.");
+ }
+ }
+ }
+}