| ofs | hex dump | ascii |
|---|
| 0000 | 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 02 96 00 00 00 94 08 06 00 00 00 29 a5 69 | .PNG........IHDR.............).i |
| 0020 | 28 00 00 00 19 74 45 58 74 53 6f 66 74 77 61 72 65 00 41 64 6f 62 65 20 49 6d 61 67 65 52 65 61 | (....tEXtSoftware.Adobe.ImageRea |
| 0040 | 64 79 71 c9 65 3c 00 00 6a 9f 49 44 41 54 78 da ec bd d7 96 1c 47 96 2d 78 dc 3d b4 4a 81 cc 84 | dyq.e<..j.IDATx......G.-x.=.J... |
| 0060 | d6 9a 0a 04 c9 2a 16 59 2c d9 77 6e 57 57 af 79 9b 5f 98 79 9e 1f 9a c7 99 35 0f f3 30 73 ab bb | .....*.Y,.wnWW.y._.y.....5..0s.. |
| 0080 | 56 af d5 7d 6f 17 4b 52 6b 90 05 45 02 48 24 90 5a 85 16 ee 3e b6 dd 8f a5 7b 06 22 32 74 ca b3 | V..}o.KRk..E.H$.Z...>....{."2t.. |
| 00a0 | 49 83 47 86 74 37 33 37 db b6 ed 08 a3 f1 bf ff 5f e7 88 c8 50 a5 ac 4a 41 95 aa 2a 36 09 0e 1c | I.G.t737........_...P..JA..*6... |
| 00c0 | 4c 27 42 ae e1 50 29 bd 4e ab 13 73 de 11 7f e3 79 c1 81 07 1a f1 82 2a ef aa f2 6b 55 5e 51 c5 | L'B..P).N..s....y......*...kU^Q. |
| 00e0 | 51 e5 6b 55 fe a4 ca c7 aa 3c e1 fb b8 d1 cb 17 3b 66 83 0c d7 a4 64 69 8c 26 d6 4f 52 aa 38 41 | Q.kU.....<......;f....di.&.OR.8A |
| 0100 | 96 8d be 64 ab 82 77 b8 e1 b7 5b aa 24 54 19 53 65 5c 95 89 d0 71 ac c5 df 93 7c cc f2 67 71 7e | ...d..w...[.$T.Se\...q....|..gq~ |
| 0120 | 9b aa 6c 84 8e 79 55 d6 f9 f1 5a 53 c1 73 35 1e 93 74 71 5a 1c 05 1d 60 39 75 8c 12 f4 69 7a 8a | ..l..yU...ZS.s5..tqZ...`9u...iz. |
| 0140 | fe cf 89 8b f4 a7 cc 71 da 34 a3 14 77 55 3b ab 29 c0 52 d5 68 b9 98 0c dc 6e bf 32 ae ca 94 2a | .......q.4..wU;.).R.h....n.2...* |
| 0160 | 27 55 c1 3c 72 86 8f 28 67 b9 dd 0d 6e e7 67 aa cc a9 f2 94 1f e3 b8 a4 ca 8a 2a 15 ee b3 28 75 | 'U.<r..(g...n.g...........*...(u |
| 0180 | 3e ba d2 62 fb 13 45 75 fb 47 d4 6d f7 06 dd a5 ff c5 fd 03 dd a6 7b 5e 9f 29 50 72 d4 3f fd 5f | >..b..Eu.G.m..........{^.)Pr.?._ |
| 01a0 | 55 f9 df 54 c9 a9 f2 ff a8 f2 ff a9 b2 d0 ef 97 61 dc 43 f7 4c 15 c7 e9 d8 ea 19 ef 88 71 d0 7f | U..T............a.C.L........q.. |
| 01c0 | 7e 20 e0 9e 78 5b 95 ff a2 ca 35 55 ee aa f2 7f ab f2 d1 70 ef 67 35 2c aa 01 fa d3 cc 0c fd 1f | ~...x[....5U.......p.g5,........ |
| 01e0 | c7 ae d0 1f d5 fd 5c 57 f7 77 d6 bb cf 87 06 dc bf bf 51 e5 7f e5 31 1c f5 fe ff 76 aa f7 08 0f | ......\W.w........Q...1....v.... |
| 0200 | cc 06 0f ce ae dc d0 02 c1 be 85 cd c4 6c 83 17 81 19 9e d4 5f e2 05 61 94 27 ec e5 9e 46 0e 8f | .............l......_..a.'...F.. |
| 0220 | 3d 1a de 80 5a 8f 56 a8 11 a9 a8 c5 48 ca 1b 64 31 1c b8 c6 b6 21 41 8f 11 36 13 80 1a 13 83 32 | =...Z.V.....H..d1....!A..6.....2 |
| 0240 | ff 7e d4 63 2f 7e d1 ef 6d f0 f9 c5 42 9f 21 7e 6f 9a 8f 9a ac 62 50 2e 71 c9 f3 b5 96 42 bf 51 | .~.c/~..m...B.!~o....bP.q....B.Q |
| 0260 | 0e 3d 0e 3f 27 8b e1 0e 70 0d bf 9d 53 6a e2 39 5b 2b d2 99 7a 89 1e c6 32 54 32 23 1e b1 8c bb | .=.?'...p...Sj.9[+..z...2T2#.... |
| 0280 | 0d c5 14 1d 8a b8 41 43 77 fa 4a ae f7 3a b7 45 91 db 0c 0b 82 24 bf 16 e7 36 4f f0 22 c3 e0 09 | ......ACw.J..:.E.....$...6O."... |
| 02a0 | 6a 46 95 55 ee ab 9b 5c f2 a1 52 94 16 db a7 fd c8 6b 42 dc e0 8e 47 30 51 dc d0 6b c6 68 28 84 | jF.U...\..R......kB...G0Q..k.h(. |
| 02c0 | c9 e3 dd 09 1e 33 e2 83 de f3 fe b8 e6 7a e2 4b d3 18 37 28 d0 f7 4f f1 42 2b 13 ba 4f 86 dc 10 | .....3.......z.K..7(..O.B+..O... |
| 02e0 | ae 57 62 ae 43 a6 3a 8e 88 bc 45 78 5c 3e c1 f7 70 b4 9b 85 7c 84 95 02 23 b4 62 14 62 29 10 ec | .Wb.C.:...Ex\>..p...|...#.b.b).. |
| 0300 | 3f 38 ac ee 7c c6 ab 45 28 95 50 2d 6f a8 f2 0b 55 5e 55 e5 73 55 fe ac ca a7 bd ad e4 4d c2 b8 | ?8..|..E(.P-o...U^U.sU.......M.. |
| 0320 | 5a 8f 95 69 33 b7 44 b6 22 98 b9 fc 34 c5 2b 69 8f 5c 62 e0 6d 22 13 35 9e fc 2b 3c 7e c4 76 28 | Z..i3.D."...4.+i.\b.m".5..+<~.v( |
| 0340 | 71 1e 8c 30 d8 a6 78 52 48 72 49 84 1e e3 b5 63 3c 10 a7 b8 98 4c 48 37 99 84 ac 70 59 0b 1d 97 | q..0..xRHrI....c<....LH7...pY... |
| 0360 | f9 f1 2a bf 4f c6 af b6 84 c0 f2 8e a7 15 a9 fc ed e6 1c 8d 29 82 f9 bb dc 19 ba 93 1c 57 03 bf | ..*.O...............)........W.. |
| 0380 | a1 08 a5 eb f5 83 1e 2a d0 e6 3a af 73 3f f8 81 db 57 97 31 6e d3 09 0a 14 6c 4c 50 d7 99 5c 1a | .......*..:.s?...W.1n....lLP..\. |
| 03a0 | bc 70 58 e0 05 11 0a 54 77 ad 6c ae 50 8f ea bb 60 f4 d0 c4 11 24 d2 56 b7 28 8a 7e ce 18 dd ed | .pX....Tw.l.P...`....$.V.(.~.... |
| 03c0 | 97 e4 85 49 2e d4 ef 0a 03 5d 87 eb 9f 31 16 d6 c3 e5 95 1e a1 fc 11 f9 aa 3d ee 89 2f 68 00 65 | ...I.....]...1...........=../h.e |
| 03e0 | b5 fd 05 f8 0b c5 86 61 7a 8b 46 63 14 4d ed df bf c7 79 3c 2e f3 38 db b1 de 23 b2 32 14 08 0e | .......az.Fc.M....y<..8...#.2... |
| 0400 | 0c b1 d4 ca ce 43 55 be 52 e5 be 2a 3f 57 e5 2d 9e b0 7f 4c fe 36 34 b6 22 ef a8 f2 88 fc 6d 47 | .....CU.R..*?W.-...L.64.".....mG |
| 0420 | 7b e7 01 d6 1f 92 6c ab a1 4a 41 4d 0d 58 01 5b 1e c3 88 d5 52 64 3a 96 b7 a2 67 82 e9 86 16 a1 | {.....l..JAM.X.[....Rd:...g..... |
| 0440 | bd c2 e2 f3 d3 5b e6 b9 a6 bf c7 79 20 3b c6 93 48 86 89 e9 04 1f 73 ac 74 95 79 dc d2 c7 22 0f | .....[.....y.;..H.....s.t.y...". |
| 0460 | 76 05 26 bc e5 a6 52 69 71 3c 7a c4 d2 f0 db 39 67 d7 28 d7 a8 79 83 7f de 8c d2 84 5d f5 c8 41 | v.&...Riq<z....9g.(..y......]..A |
| 0480 | c5 b4 e8 71 2c 43 cb 91 78 a0 60 ba 8e 47 38 a9 35 63 47 bf d2 ea f2 4a 9b 9f 9d e2 be 89 49 f6 | ...q,C..x.`..G8.5cG....J......I. |
| 04a0 | 34 1f cf b0 a2 33 c6 6d 1a 0d a9 51 97 99 6c ae 86 16 0e 79 7a 51 d1 ac ca 90 b0 37 80 c9 84 a2 | 4....3.m...Q..l....yzQ.....7.... |
| 04c0 | 32 6a 25 91 a5 bb 6a a8 c9 a9 db ef 2c 2d 2a e6 51 51 83 82 a5 56 9d d1 61 2b 97 16 f7 95 34 df | 2j%...j.....,-*.QQ...V..a+....4. |
| 04e0 | eb 58 c4 2c f6 7b 1f 63 2c 03 91 b4 ec 18 45 eb 09 4a 54 b2 de e3 16 bb 33 bd 42 ab aa 97 b8 1f | .X.,.{.c,.....E..JT.....3.B..... |
| 0500 | bb bc c8 ff 1b 9f ef 70 d8 1e df 8f 9b 56 9c 9e 47 d3 f4 55 72 82 96 d5 63 a8 96 91 e1 12 fb 04 | .......p.....V..G..Ur...c....... |
| 0520 | df 93 63 7c 4f ce f1 7c 52 ee 86 58 0a 04 82 83 87 75 5e 09 af 30 89 7c 9d 7c d5 f2 4d f2 95 cc | ..c|O..|R..X.....u^..0.|.|..M... |
| 0540 | 6f 55 f9 8b 2a 7f 55 e5 7b ea 42 88 f2 d5 49 97 6a f1 32 6d 1a 4b e4 18 36 e5 f2 33 14 af a4 5a | oU..*.U.{.B...I.j.2m.K..6..3...Z |
| 0560 | 29 97 fd c0 e6 f3 5d e7 15 7c 3c 54 12 b4 5d c1 4c 84 54 2f ad 80 a5 42 65 82 89 8a 7e 3e c1 e4 | ).....]..|<T..].L.T/...Be...~>.. |
| 0580 | bb c4 df bf 42 db 15 ce 65 7e 7e 99 02 95 b3 7e 24 7b 8e e1 5b 29 9c ad fb ca e5 4f 8a 0b 1e 55 | ....B...e~~....~${..[).....O...U |
| 05a0 | b8 97 1c a7 7f c9 9d a6 d5 48 ac 5f 05 b3 15 34 31 44 7b c3 d6 2c cb 25 c7 6d 38 1d 5a 54 e0 b9 | .........H._...41D{..,.%.m8.ZT.. |
| 05c0 | 0b fc 38 1d ea 2f cf 55 99 e5 89 0d c7 a7 fc 78 43 86 81 dd 47 d4 eb 1d ae 62 18 53 f4 be f1 ba | ..8../.U.......xC...G....b.S.... |
| 05e0 | 62 7a 49 fa a5 fb 19 5d 54 cf 60 5b 1c c4 d2 18 3e c1 39 ce fd a3 c8 7d 69 00 b5 12 5b df 26 c5 | bzI....]T.`[....>.9....}i...[.&. |
| 0600 | aa 09 6f 67 06 b6 95 20 98 58 5f 1b ee 40 63 1c fa 35 76 90 5e e2 31 0c fd f6 ef dc 67 6b 43 bb | ..og.....X_..@c..5v.^.1.....gkC. |
| 0620 | 7d 71 5b a8 f1 78 4e 2d 02 ff 2d 7b 8a fe 92 99 a1 a7 8a 60 46 bc 3d 89 a1 ee b8 a3 be cf f3 22 | }q[..xN-..-{.......`F.=........" |
| 0640 | 1f 63 e8 e3 6e ef 39 21 96 02 c1 c1 c5 06 97 07 e4 ab 93 98 c4 df e1 c9 f9 25 26 5c b0 c1 fc 86 | .c..n.9!.................%&\.... |
| 0660 | 7c 95 73 6e a7 01 19 e4 11 44 02 b6 96 d5 78 d1 b3 df 81 72 09 eb 9d 78 35 e5 39 f4 78 9b 5d 9e | |.sn.....D....x....r...x5.9.x.]. |
| 0680 | 4d d2 40 76 49 36 05 4a 63 27 a4 29 50 32 27 29 d8 5a d5 c7 29 7e 3c ce 83 79 9a c7 35 4d 3e 4f | M.@vI6.Jc'.)P2').Z..)~<..y..5M>O |
| 06a0 | 31 d9 6c 56 38 b5 f3 50 21 a4 ba 35 2b a1 e5 c3 d8 69 6c c3 f2 36 b9 a2 6a 12 bd 5a 55 55 c0 4e | 1.lV8..P!..5+....il..6..j..ZUU.N |
| 06c0 | 3d d3 76 8d d6 cc 28 65 d5 d1 53 30 ad 08 3d 56 13 d6 ba 15 f5 48 66 d2 f5 27 ad 3e 5a 1d ea e2 | =.v...(e..S0..=V.....Hf..'.>Z... |
| 06e0 | 12 97 66 25 ea 38 b7 d1 d9 d0 f1 0c f7 db 0c b7 61 8c 27 39 3c 7f 3d b4 70 c0 64 a7 b7 e6 36 43 | ..f%.8..........a.'9<.=.p.d...6C |
| 0700 | c7 3c 89 cd ed c8 60 b1 89 5d 41 dd 62 0f 54 b1 d5 33 49 aa 78 c7 13 aa 59 40 2e 41 2c 6d 4f c0 | .<....`..]A.b.T..3I.x...Y@.A,mO. |
| 0720 | 1b 0a 52 dc 4f 32 14 98 4e 94 fa fd 32 5f b1 74 3d 95 12 6a 25 8a 6f 63 5e 67 67 c5 be 81 71 e8 | ..R.O2..N...2_.t=..j%.oc^gg...q. |
| 0740 | ed 50 1f fd 82 17 f5 43 55 d7 0d 07 c4 52 11 78 d3 a2 ef 12 e3 f4 65 72 c2 53 88 c7 d5 7d 6b d0 | .P.....CU....R.x......er.S...}k. |
| 0760 | 50 6d 81 72 7c 3f 8e f3 7d f6 84 ef 2d 21 96 02 c1 11 00 b6 84 be e3 9b 1e aa d0 cb e4 ab 96 d8 | Pm.r|?..}...-!.................. |
| 0780 | 92 81 57 22 b6 c8 3f 20 df 7b fc eb 4e 83 72 2b e5 d2 a0 19 b6 b9 24 de 16 df b5 6b d3 44 70 95 | ..W"..?..{..N.r+......$....k.Dp. |
| 07a0 | 02 45 b3 f9 18 b6 df 0c 2b 9c 09 9e 8c 12 4c 54 d2 14 d8 70 9a 5c 6f 2b 21 d2 b3 cc 93 16 8e 8b | .E......+.....LT...p.\o+!....... |
| 07c0 | 14 78 2d 1f ca 6d 57 df 77 21 70 bb f0 14 cc fc 33 7a 87 15 cc bb ac 60 ae 47 26 7c 8d ca 1d 3a | .x-..mW.w!p.....3z.....`.G&|...: |
| 07e0 | 57 c3 17 3e a3 60 9b 2d c3 93 59 96 02 f3 08 bd 80 d0 26 13 27 f9 98 e0 76 59 a0 ed 8a a6 b6 d1 | W..>.`.-..Y.......&.'...vY...... |
| 0800 | 7c 7e 58 17 06 7b de 6f bc a5 a6 e1 11 48 6c 7d cf aa b1 e1 0f c6 1b 9e 72 f9 33 fusing Google.Protobuf;
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.Core;
using Tango.Integration.ExternalBridge;
using Tango.PMR.Diagnostics;
using Tango.Integration.Operation;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Authentication;
using Tango.PPC.Common.Connection;
using Tango.Transport;
namespace Tango.PPC.Common.EventLogging
{
/// <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 IMachineProvider _machineProvider;
private IAuthenticationProvider _authentication;
private Dictionary<EventTypes, BL.Entities.EventType> _eventTypesGuids;
private String _hostName;
private bool _isInitialized;
private List<MachinesEvent> _pendingEvents;
private List<MachinesEvent> _currentEvents;
#region Events
/// <summary>
/// Occurs when a new machine event has been received.
/// </summary>
public event EventHandler<MachinesEvent> EventReceived;
/// <summary>
/// Occurs when a machine event has been resolved.
/// </summary>
public event EventHandler<MachinesEvent> EventResolved;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="DefaultEventLogger"/> class.
/// </summary>
/// <param name="machineProvider">The application manager.</param>
/// <param name="authenticationProvider">The authentication provider.</param>
public DefaultEventLogger(IMachineProvider machineProvider, IAuthenticationProvider authenticationProvider)
{
_hostName = Environment.MachineName;
_events = new ConcurrentQueue<MachinesEvent>();
_pendingEvents = new List<MachinesEvent>();
_currentEvents = new List<MachinesEvent>();
_eventTypesGuids = new Dictionary<EventTypes, BL.Entities.EventType>();
_machineProvider = machineProvider;
_authentication = authenticationProvider;
_logThread = new Thread(LogThreadMethod);
_logThread.IsBackground = true;
_logThread.Start();
_machineProvider.MachineOperator.MachineEventsStateProvider.NewEvents += MachineEventsStateProvider_NewEvents;
_machineProvider.MachineOperator.MachineEventsStateProvider.EventsResolved += MachineEventsStateProvider_EventsResolved;
_machineProvider.MachineOperator.RequestSent += Machine_RequestSent;
_machineProvider.MachineOperator.RequestFailed += Machine_RequestFailed;
_machineProvider.MachineOperator.ResponseReceived += Machine_ResponseReceived;
_machineProvider.MachineOperator.StateChanged += MachineOperator_StateChanged;
_machineProvider.MachineOperator.PrintingStarted += MachineOperator_PrintingStarted;
_machineProvider.MachineOperator.PrintingAborted += MachineOperator_PrintingAborted;
_machineProvider.MachineOperator.PrintingCompleted += MachineOperator_PrintingCompleted;
_machineProvider.MachineOperator.PrintingFailed += MachineOperator_PrintingFailed;
}
#endregion
#region Private Methods
private void Init()
{
if (!_isInitialized)
{
try
{
_db = ObservablesContext.CreateDefault();
_db.EventTypes.ToList();
foreach (var type in _db.EventTypes)
{
_eventTypesGuids.Add((EventTypes)type.Code, type);
}
_isInitialized = true;
}
catch
{
_isInitialized = false;
}
}
}
#endregion
#region Event Handlers
/// <summary>
/// Handles the PrintingFailed event of the MachineOperator.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PrintingFailedEventArgs"/> instance containing the event data.</param>
private void MachineOperator_PrintingFailed(object sender, PrintingFailedEventArgs e)
{
Log(EventTypes.JOB_FAILED, e.Exception.Message);
}
/// <summary>
/// Handles the PrintingCompleted event of the MachineOperator.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PrintingEventArgs"/> instance containing the event data.</param>
private void MachineOperator_PrintingCompleted(object sender, PrintingEventArgs e)
{
Log(EventTypes.JOB_COMPLETED, $"Job '{e.Job.Name}' completed successfully.");
}
/// <summary>
/// Handles the PrintingAborted event of the MachineOperator.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PrintingEventArgs"/> instance containing the event data.</param>
private void MachineOperator_PrintingAborted(object sender, PrintingEventArgs e)
{
Log(EventTypes.JOB_ABORTED, $"Job '{e.Job.Name}' has been aborted.");
}
/// <summary>
/// Handles the PrintingStarted event of the MachineOperator.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PrintingEventArgs"/> instance containing the event data.</param>
private void MachineOperator_PrintingStarted(object sender, PrintingEventArgs e)
{
Log(EventTypes.JOB_STARTED, $"Job '{e.Job.Name}' started.");
}
/// <summary>
/// Handles the machine operator state changed event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void MachineOperator_StateChanged(object sender, Transport.TransportComponentState e)
{
if (e == Transport.TransportComponentState.Connected)
{
_events = new ConcurrentQueue<MachinesEvent>();
_currentEvents.Clear();
}
}
/// <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.REQUEST_SENT, 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.REQUEST_FAILED, 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.RESPONSE_RECEIVED, 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<MachinesEvent> events)
{
foreach (var ev in events)
{
Log(ev);
}
}
/// <summary>
/// Handles the connected machine events state provider EventsResolved event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="events">The events.</param>
private void MachineEventsStateProvider_EventsResolved(object sender, IEnumerable<MachinesEvent> events)
{
foreach (var ev in events)
{
//Log(String.Format("Event '{0}' resolved.", ev.EventType.Name));
var existing = _currentEvents.SingleOrDefault(x => x.Type == ev.Type);
if (existing != null)
{
_currentEvents.Remove(existing);
EventResolved?.Invoke(this, existing);
}
}
}
#endregion
#region Logging
/// <summary>
/// Logs the specified machine event.
/// </summary>
/// <param name="machineEvent">The machine event.</param>
public void Log(MachinesEvent machineEvent)
{
machineEvent.HostName = _hostName;
machineEvent.EventType = _eventTypesGuids[machineEvent.Type];
if (_machineProvider.Machine == null || _authentication.CurrentUser == null)
{
_pendingEvents.Add(machineEvent);
}
else
{
if (_pendingEvents.Count > 0)
{
var pending = _pendingEvents.ToList();
_pendingEvents.Clear();
foreach (var ev in pending)
{
Log(ev);
}
}
LogManager.Log("Logging event " + machineEvent.EventType.Name);
machineEvent.MachineGuid = _machineProvider.Machine.Guid;
machineEvent.UserGuid = _authentication.CurrentUser.Guid;
machineEvent.User = _authentication.CurrentUser;
_events.Enqueue(machineEvent);
if (!_currentEvents.Exists(x => x.Type == machineEvent.Type))
{
if (machineEvent.Group != EventTypeGroups.Application && machineEvent.Group != EventTypeGroups.Transport)
{
_currentEvents.Add(machineEvent);
}
}
EventReceived?.Invoke(this, 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)
{
Init();
MachinesEvent machineEvent = new MachinesEvent();
machineEvent.DateTime = DateTime.UtcNow;
machineEvent.Description = message;
machineEvent.EventType = _eventTypesGuids[eventType];
machineEvent.EventTypeGuid = machineEvent.EventType.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.APPLICATION_EXCEPTION, exception.FlattenMessage());
}
/// <summary>
/// Logs the specified exception using the <see cref="EventTypes.ApplicationException" />.
/// </summary>
/// <param name="exception">The exception.</param>
/// <param name="description"></param>
public void Log(Exception exception, string description)
{
Log(EventTypes.APPLICATION_EXCEPTION, description + Environment.NewLine + exception.FlattenMessage());
}
/// <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.APPLICATION_INFORMATION, message);
}
/// <summary>
/// Logging thread loop.
/// </summary>
private void LogThreadMethod()
{
while (true)
{
FlushAll();
Thread.Sleep(5000);
}
}
/// <summary>
/// Immediately saves all pending events to database.
/// </summary>
public void FlushAll()
{
bool _saveChanges = false;
while (_events.Count > 0)
{
MachinesEvent ev = null;
if (_events.TryDequeue(out ev))
{
ev.User = null;
_db.MachinesEvents.Add(ev);
_saveChanges = true;
}
}
if (_saveChanges)
{
try
{
_db.SaveChanges();
}
catch (Exception ex)
{
LogManager.Log(ex, "Error saving machine event to database.");
}
}
}
#endregion
}
}
|