Exemple #1
0
        public async Task TestProtoBufEnumerationSerialization()
        {
            Trace.WriteLine(RuntimeTypeModel.Default.GetSchema(typeof(IEnumerable <Log>)));
            const int testCount = 5;

            Log[] logs = new Log[testCount];
            for (int m = 0; m < testCount; m++)
            {
                logs[m] = new Log(
                    new LogContext().Set("Test No", m),
                    new Exception("Exception", new Exception("Inner exception")),
                    LoggingLevel.Information,
                    () => Resources.TestString,
                    m,
                    Guid.NewGuid())
                          .Add();
            }

            await Log.Flush().ConfigureAwait(false);

            CollectionAssert.AllItemsAreNotNull(logs);
            Assert.IsTrue(logs.All(l => l.MessageFormat == Resources.TestString), "Logs contain incorrect message format.");

            IEnumerable <Log> logs2;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                TestLogEnumeration test = new TestLogEnumeration(logs);
                Serializer.Serialize(memoryStream, test);

                Trace.WriteLine(string.Format("Serialized logs took up {0} bytes.", memoryStream.Position));
                memoryStream.Seek(0, SeekOrigin.Begin);

                //logs2 = Serializer.Deserialize<IEnumerable<Log>>(memoryStream); This doesnt work with the Trace.WriteLine at the top
                TestLogEnumeration test2 = Serializer.Deserialize <TestLogEnumeration>(memoryStream);
                logs2 = test2.Logs;
                Assert.AreEqual(memoryStream.Position, memoryStream.Length);
            }
            Assert.IsNotNull(logs2);
            List <Log> result = logs2.ToList();

            Assert.AreEqual(logs.Length, result.Count);
            for (int i = 0; i < logs.Length; i++)
            {
                Assert.AreEqual(logs[i].ToString(), result[i].ToString());
            }
        }
        public async Task TestProtoBufEnumerationSerialization()
        {
            Trace.WriteLine(RuntimeTypeModel.Default.GetSchema(typeof(IEnumerable<Log>)));
            const int testCount = 5;

            Log[] logs = new Log[testCount];
            for (int m = 0; m < testCount; m++)
                logs[m] = new Log(
                    new LogContext().Set("Test No", m),
                    new Exception("Exception", new Exception("Inner exception")),
                    LoggingLevel.Information,
                    () => Resources.TestString,
                    m,
                    Guid.NewGuid())
                    .Add();

            await Log.Flush().ConfigureAwait(false);

            CollectionAssert.AllItemsAreNotNull(logs);
            Assert.IsTrue(logs.All(l => l.MessageFormat == Resources.TestString), "Logs contain incorrect message format.");

            IEnumerable<Log> logs2;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                TestLogEnumeration test = new TestLogEnumeration(logs);
                Serializer.Serialize(memoryStream, test);

                Trace.WriteLine(string.Format("Serialized logs took up {0} bytes.", memoryStream.Position));
                memoryStream.Seek(0, SeekOrigin.Begin);

                //logs2 = Serializer.Deserialize<IEnumerable<Log>>(memoryStream); This doesnt work with the Trace.WriteLine at the top
                TestLogEnumeration test2 = Serializer.Deserialize<TestLogEnumeration>(memoryStream);
                logs2 = test2.Logs;
                Assert.AreEqual(memoryStream.Position, memoryStream.Length);
            }
            Assert.IsNotNull(logs2);
            List<Log> result = logs2.ToList();
            Assert.AreEqual(logs.Length, result.Count);
            for (int i = 0; i < logs.Length; i++)
                Assert.AreEqual(logs[i].ToString(), result[i].ToString());
        }