Beispiel #1
0
        public EventLogCollector(CollectorID id, Remote remote_info, ECollectorType type)
            : base(new DataCollectorContext(id, type))
        {
            RemoteInfo = remote_info;

            m_machine_name = Context.ID.Name;
            CollectorType  = type;

            if (type != ECollectorType.SystemErrors && type != ECollectorType.ApplicationErrors)
            {
                throw new Exception($"EventLogCollector: invalid collector type {type}");
            }

            // So we don't keep getting log entries that have already been retrieved, keep track
            // of the RecordNumber, which is an ever-increasing number for each of the different
            // types of logfiles (System, or Application), and the dates.

            // We need to keep the dates because if a particular machine is rebuilt, the RecordNumber
            // will get reset and we'll stop getting any errors from the event log.
            // Originally, we just kept the record number, but that's not good enough, so we
            // switched to using the date as the primary clause.

            m_log_data = new EventLogData();

            SystemErrorsInfoRequest.EType req_type = SystemErrorsInfoRequest.EType.System;
            switch (CollectorType)
            {
            case ECollectorType.ApplicationErrors:
                req_type = SystemErrorsInfoRequest.EType.Application;
                break;

            case ECollectorType.SystemErrors:
                req_type = SystemErrorsInfoRequest.EType.System;
                break;

            default:
                throw new Exception($"EventLogCollector: failure to handle type conversion from {CollectorType}");
            }

            SystemErrorsInfoRequest request = new SystemErrorsInfoRequest(m_machine_name, req_type);

            RequestBus.Instance.MakeRequest(request);
            if (request.IsHandled)
            {
                m_log_data.Assign(request.LogData);
            }
        }
        public void ConvertProperly(string section, SystemErrorsInfoRequest.EType type)
        {
            using (FileDeleter fd = new FileDeleter(Extensions.GetTempDBFile()))
            {
                Database    db   = new Database(new Context(fd.Fi));
                Initializer init = new Initializer(null);
                init.Initialize(db);

                RequestBus        bus       = new RequestBus();
                EventLogResponder responder = new EventLogResponder()
                {
                    DB = db
                };
                bus.Subscribe(responder);

                using (SQLiteConnection conn = db.Connection)
                {
                    conn.Open();

                    string orig_path  = $"machine_name.{section}.max_record_number";
                    string new_path   = $"machine_name.{section}.max_event_log";
                    ulong  record_num = 234567;

                    Attribute attr = new Attribute();
                    attr.Set(orig_path, record_num.ToString(), conn);

                    SystemErrorsInfoRequest req = new SystemErrorsInfoRequest("machine_name", type);
                    bus.MakeRequest(req);
                    Assert.True(req.IsHandled);
                    Assert.True(req.LogData.MaxRecordNumber.HasValue);
                    Assert.Equal(record_num, req.LogData.MaxRecordNumber.Value);

                    string x = attr.Get(orig_path, conn);
                    Assert.True(string.IsNullOrEmpty(x));

                    x = attr.Get(new_path, conn);
                    Assert.False(string.IsNullOrEmpty(x));

                    EventLogData eld = JsonConvert.DeserializeObject <EventLogData>(x);
                    Assert.NotNull(eld);
                    Assert.True(eld.MaxRecordNumber.HasValue);
                    Assert.Equal(record_num, eld.MaxRecordNumber.Value);
                }
            }
        }