blob: c22ce335c80b26adc4e62e7d64b4c22cbefee1d2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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.");
}
}
}
}
|