Centreon_EventLog_2_Syslog.ThreadFilter.ThreadLoop C# (CSharp) Method

ThreadLoop() public method

Scan Windows eventLog for find event like rules and send it to a syslog server
public ThreadLoop ( Object threadContext ) : void
threadContext Object ManualResetEvent object
return void
        public void ThreadLoop(Object threadContext)
        {
            EventLog eventLog = null;
            EventLogEntryCollection eventLogEntryCollection = null;
            EventLogEntry eventLogEntry = null;

            try
            {
                // Get EventLog entries
                eventLog = new EventLog();
                eventLog.Log = _LogName;
                eventLog.MachineName = ".";
                eventLogEntryCollection = eventLog.Entries;
            }
            catch (System.Exception e)
            {
                this._Debug.Write("Thread " + _LogName, "Unable to load eventLog: \"" + _LogName + "\" entries because : " + e.Message, DateTime.Now, 1);
            }

            try
            {
                int NbLogEntries = eventLogEntryCollection.Count;

                this._Debug.Write("Thread " + _LogName, "Start events control from: " + _LogName, DateTime.Now, 2);

                for (int i = NbLogEntries - 1; i > 0; i--)
                {
                    eventLogEntry = eventLogEntryCollection[i];

                    long elapsedTimeMin = eventLogEntry.TimeWritten.Ticks - _LastExecTime.Ticks;
                    long elapsedTimeMax = eventLogEntry.TimeWritten.Ticks - _MaxExecTime.Ticks;

                    // If event are too old (more than last check) break loop
                    if (elapsedTimeMin < 0) break;

                    // While event is contained between last check and program time execution
                    if ((elapsedTimeMin >= 0) && (elapsedTimeMax < 0))
                    {
                        Boolean isExclude = false;
                        Boolean isInclude = false;

                        try
                        {
                            if (this._eFilters != null)
                            {
                                isExclude = TestEvent(eventLogEntry, this._eFilters);
                            }
                        }
                        catch (Exception e)
                        {
                            this._Debug.Write("Thread " + _LogName, "Problem during check if event is exclude: \"" + _LogName + "\" entries because : " + e.Message, DateTime.Now, 1);
                        }

                        if (!isExclude)
                        {
                            try
                            {
                                if (this._iFilters != null)
                                {
                                    isInclude = TestEvent(eventLogEntry, this._iFilters);
                                }
                            }
                            catch (Exception e)
                            {
                                this._Debug.Write("Thread " + _LogName, "Problem during check if event is include: \"" + _LogName + "\" entries because : " + e.Message, DateTime.Now, 1);
                            }
                        }

                        if (isInclude)
                        {
                            try
                            {
                                this._SyslogServer.SendEvent(this._LogName, eventLogEntry, iFilter);
                            }
                            catch (Exception e)
                            {
                                this._Debug.Write("Thread " + _LogName, "Unable to send eventLogEntry to syslogServer process because : " + e.Message, DateTime.Now, 1);
                            }
                        }
                    }
                    else if (elapsedTimeMin >= 0)
                    {
                        break;
                    }
                }

                // Desctruct all
                eventLog = null;
                eventLogEntry = null;
                eventLogEntryCollection = null;

                this._Debug.Write("Thread " + _LogName, "Finish events control", DateTime.Now, 2);
                _DoneEvent.Set();
            }
            catch (System.Exception e)
            {
                this._Debug.Write("Thread " + _LogName, "Problem during research due to: " + e.Message, DateTime.Now, 1);
                _DoneEvent.Set();
            }
        }