コード例 #1
0
        public void Test()
        {
            string       message = "This is a test of serialized event data.";
            Exception    myError = new PlatformNotSupportedException("Error.Message", new InsufficientMemoryException());
            LogEventArgs arg1    = null;

            LogEventHandler eh = new LogEventHandler(delegate(object s, LogEventArgs e) { arg1 = e; });

            string[] stack = new string[] { "step 1", "step 2" };
            using (Log.Start(stack[0]))
                using (Log.Start(stack[1]))
                {
                    Log.LogWrite += eh;
                    Log.Error(myError, message);
                    Log.LogWrite -= eh;
                }
            Assert.IsNotNull(arg1);
            Assert.AreEqual(1, arg1.Count);
            Assert.AreEqual(1, arg1.ToArray().Length);

            EventData data = arg1.ToArray()[0];

            Assert.IsNotNull(data);
            BasicLogTest.AssertMessage(GetType(), stack, data, LogLevels.Error, message, myError.GetType());
            Assert.AreEqual(String.Join("::", stack), data.ToString("{LogStack}"));

            BinaryFormatter ser = new BinaryFormatter();
            MemoryStream    ms  = new MemoryStream();

            ser.Serialize(ms, arg1);
            Assert.Greater((int)ms.Position, 0);

            ms.Position = 0;
            object restored = ser.Deserialize(ms);

            Assert.IsNotNull(restored);
            Assert.AreEqual(typeof(LogEventArgs), restored.GetType());
            LogEventArgs arg2 = restored as LogEventArgs;

            Assert.IsNotNull(arg2);
            Assert.AreEqual(1, arg2.Count);
            Assert.AreEqual(1, arg2.ToArray().Length);

            data = arg2.ToArray()[0];
            Assert.IsNotNull(data);

            Assert.IsNotNull(data.Exception);
            Assert.AreNotEqual(myError.GetType(), data.Exception.GetType());
            Assert.AreEqual(typeof(Log).Assembly, data.Exception.GetType().Assembly);
            Assert.AreEqual(myError.Message, data.Exception.Message);
            Assert.AreEqual(myError.StackTrace, data.Exception.StackTrace);
            Assert.AreEqual(myError.Source, data.Exception.Source);
            Assert.AreEqual(myError.ToString(), data.Exception.ToString());

            BasicLogTest.AssertMessage(GetType(), stack, data, LogLevels.Error, message, data.Exception.GetType());
        }
コード例 #2
0
        public void TestBadMessageData()
        {
            Log.LogWrite += BreakIn_LogWrite;
            try
            {
                string       message = "Bad {500} Data: {0}";
                Exception    myError = i_blow_up;
                LogEventArgs arg1    = null;

                LogEventHandler eh = new LogEventHandler(delegate(object s, LogEventArgs e) { arg1 = e; });
                Log.LogWrite += eh;
                Log.LogWrite += BreakIn_LogWrite;
                Log.Error(myError, message, i_blow_up);
                Log.LogWrite -= eh;
                Log.LogWrite -= BreakIn_LogWrite;

                Assert.IsNotNull(arg1);
                Assert.AreEqual(1, arg1.Count);
                Assert.AreEqual(1, arg1.ToArray().Length);

                EventData data = arg1.ToArray()[0];
                Assert.IsNotNull(data);
                BasicLogTest.AssertMessage(GetType(), null, data, LogLevels.Error, null, myError.GetType());

                Assert.IsTrue(data.ToString().Contains(message));
                Assert.IsTrue(data.ToString("{Message:%s!} {}").Contains(message));
                Assert.IsTrue(data.ToString("{Exception}").Contains(myError.GetType().FullName));

                data.Write((System.Xml.XmlTextWriter)null);
                System.Xml.XmlTextWriter wtr = new System.Xml.XmlTextWriter(new MemoryStream(new byte[10]), System.Text.Encoding.UTF32);
                data.Write(wtr);

                data.Write(new StreamWriter(new MemoryStream(new byte[10])));
                foreach (string format in badFormats)
                {
                    data.Write(new StreamWriter(new MemoryStream(new byte[10])), format);
                }

                BinaryFormatter ser = new BinaryFormatter();
                MemoryStream    ms  = new MemoryStream();

                ser.Serialize(ms, arg1);
                Assert.Greater((int)ms.Position, 0);

                ms.Position = 0;
                object restored = ser.Deserialize(ms);
                Assert.IsNotNull(restored);
                Assert.AreEqual(typeof(LogEventArgs), restored.GetType());
                LogEventArgs arg2 = restored as LogEventArgs;

                Assert.IsNotNull(arg2);
                Assert.AreEqual(1, arg2.Count);
                Assert.AreEqual(1, arg2.ToArray().Length);

                data = arg2.ToArray()[0];
                Assert.IsNotNull(data);

                Assert.IsNotNull(data.Exception);
                Assert.AreNotEqual(myError.GetType(), data.Exception.GetType());
                Assert.AreEqual(typeof(Log).Assembly, data.Exception.GetType().Assembly);
                Assert.IsTrue(data.Exception.Message.Contains(myError.GetType().FullName));
                Assert.IsNotNull(data.Exception.ToString());
                Assert.AreNotEqual(String.Empty, data.Exception.ToString());
                Assert.IsTrue(data.Exception.ToString().Contains(myError.GetType().FullName));

                BasicLogTest.AssertMessage(GetType(), null, data, LogLevels.Error, null, data.Exception.GetType());

                System.Runtime.Serialization.SerializationInfo info = new System.Runtime.Serialization.SerializationInfo(data.Exception.GetType(), new myconverter());
                System.Runtime.Serialization.StreamingContext  ctx  = new System.Runtime.Serialization.StreamingContext();

                Exception err = (Exception)
                                data.Exception.GetType().InvokeMember(null, System.Reflection.BindingFlags.CreateInstance,
                                                                      null, null, new object[] { info, ctx });

                Assert.IsNotNull(err.Message);
                Assert.IsNotEmpty(err.Message);
                Assert.IsNotNull(err.ToString());
                Assert.IsNotEmpty(err.ToString());
            }
            finally
            {
                Log.LogWrite -= BreakIn_LogWrite;
                Log.LogWrite -= BreakIn_LogWrite;
                Log.LogWrite -= BreakIn_LogWrite;
            }
        }