示例#1
0
        public void TestEventDataSerialization()
        {
            //const string cvText = "CV:1234";

            var variables = new KeyValuePair <string, object>[]
            {
                new KeyValuePair <string, object>("string", "string.Value"),
                new KeyValuePair <string, object>("int", 33),
                new KeyValuePair <string, object>("long", 101),
                new KeyValuePair <string, object>("date", DateTime.Now),
            };

            var             data   = Utility.CreateData(1);
            EventDataRecord record = data.ConvertTo();

            var output          = new OutputBuffer();
            var writer          = new SimpleBinaryWriter <OutputBuffer>(output);
            var writeSerializer = new Serializer <SimpleBinaryWriter <OutputBuffer> >(typeof(EventDataRecord));

            writeSerializer.Serialize(record, writer);

            var input          = new InputBuffer(output.Data);
            var reader         = new SimpleBinaryReader <InputBuffer>(input);
            var readSerializer = new Deserializer <SimpleBinaryReader <InputBuffer> >(typeof(EventDataRecord));

            EventDataRecord resultRecord = readSerializer.Deserialize <EventDataRecord>(reader);
            EventData       result       = resultRecord.ConverTo();

            Utility.VerifyEventData(data, result);
        }
        public static EventDataRecord ConvertTo(this EventData self)
        {
            var record = new EventDataRecord
            {
                Timestamp       = self.Timestamp.ToString("o"),
                EventName       = self.EventName,
                EventSourceName = self.EventSourceName,
                EventLevel      = self.TelemetryLevel.ToString(),
                Message         = self.Message,
                Tag             = self.Tag,
                Cv    = self.Cv,
                Value = self.Value,
            };

            if (self.Properties != null)
            {
                int propertyNumber = -1;
                foreach (var item in self.Properties)
                {
                    propertyNumber++;

                    switch (item.Value)
                    {
                    case int x:
                        record.IntProperties = record.IntProperties ?? new List <PropertyDataInt>();
                        record.IntProperties.Add(new PropertyDataInt {
                            PropertyName = item.Key, Value = x
                        });
                        break;

                    case long x:
                        record.LongProperties = record.LongProperties ?? new List <PropertyDataLong>();
                        record.LongProperties.Add(new PropertyDataLong {
                            PropertyName = item.Key, Value = x
                        });
                        break;

                    case DateTime x:
                        record.DateProperties = record.DateProperties ?? new List <PropertyDataDate>();
                        record.DateProperties.Add(new PropertyDataDate {
                            PropertyName = item.Key, Value = x.ToString("o")
                        });
                        break;

                    case string x:
                        record.StringProperties = record.StringProperties ?? new List <PropertyDataString>();
                        record.StringProperties.Add(new PropertyDataString {
                            PropertyName = item.Key, Value = x
                        });
                        break;

                    default:
                        throw new InvalidOperationException($"Type={item.Value.GetType().Name} is not support");
                    }
                }
            }

            return(record);
        }
示例#3
0
        public IEventDataWriter Write(EventData eventDataItem)
        {
            Verify.IsNotNull(nameof(eventDataItem), eventDataItem);
            Verify.Assert(_file != null, "Not open");

            EventDataRecord record = eventDataItem.ConvertTo();

            _serializer.Serialize(record, _binaryWriter);

            return(this);
        }
示例#4
0
        public Task Save(IEnumerable <EventData> eventDataItems)
        {
            Verify.IsNotNull(nameof(eventDataItems), eventDataItems);
            Verify.IsNotNull(nameof(_file), _file);

            if (eventDataItems.Count() == 0)
            {
                return(Task.FromResult(0));
            }

            foreach (var item in eventDataItems)
            {
                EventDataRecord record = item.ConvertTo();
                _serializer.Serialize(record, _simpleBinaryWriter);
            }

            return(Task.FromResult(0));
        }
        public static EventData ConverTo(this EventDataRecord record)
        {
            List <KeyValuePair <string, object> > properties = new List <KeyValuePair <string, object> >();

            process(record.IntProperties?.Count, index => new KeyValuePair <string, object>(record.IntProperties[index].PropertyName, record.IntProperties[index].Value));
            process(record.LongProperties?.Count, index => new KeyValuePair <string, object>(record.LongProperties[index].PropertyName, record.LongProperties[index].Value));
            process(record.StringProperties?.Count, index => new KeyValuePair <string, object>(record.StringProperties[index].PropertyName, record.StringProperties[index].Value));

            process(record.DateProperties?.Count, index =>
            {
                DateTime dateTime = DateTime.Parse(record.DateProperties[index].Value, null, DateTimeStyles.RoundtripKind);
                return(new KeyValuePair <string, object>(record.DateProperties[index].PropertyName, dateTime));
            });

            return(new EventData(
                       timestamp: DateTime.Parse(record.Timestamp, null, DateTimeStyles.RoundtripKind),
                       eventName: record.EventName,
                       eventSourceName: record.EventSourceName,
                       telemetryLevel: (TelemetryLevel)Enum.Parse(typeof(TelemetryLevel), record.EventLevel),
                       message: record.Message,
                       tag: record.Tag,
                       cv: record.Cv,
                       value: record.Value,
                       properties: properties
                       ));

            // Local method for processing properties
            void process(int?count, Func <int, KeyValuePair <string, object> > getData)
            {
                if (count == null)
                {
                    return;
                }

                for (int i = 0; i < count; i++)
                {
                    properties.Add(getData(i));
                }
            };
        }
示例#6
0
        public void TestMultiple()
        {
            const int count = 10;

            var eventDataItems = new List <EventData>();
            var rnd            = new Random();

            var output          = new OutputBuffer();
            var writer          = new SimpleBinaryWriter <OutputBuffer>(output);
            var writeSerializer = new Serializer <SimpleBinaryWriter <OutputBuffer> >(typeof(EventDataRecord));

            foreach (var index in Enumerable.Range(0, count))
            {
                var             data   = Utility.CreateData(index);
                EventDataRecord record = data.ConvertTo();

                eventDataItems.Add(data);
                writeSerializer.Serialize(record, writer);
            }

            var input          = new InputBuffer(output.Data);
            var reader         = new SimpleBinaryReader <InputBuffer>(input);
            var readSerializer = new Deserializer <SimpleBinaryReader <InputBuffer> >(typeof(EventDataRecord));

            foreach (var index in Enumerable.Range(0, count))
            {
                EventDataRecord resultRecord = readSerializer.Deserialize <EventDataRecord>(reader);
                EventData       result       = resultRecord.ConverTo();

                Utility.VerifyEventData(eventDataItems[index], result);
            }

            Action act = () => Deserialize <EventDataRecord> .From(reader);

            act.Should().Throw <EndOfStreamException>();
        }