Example #1
0
 public void TestFormatEvent()
 {
     Formatter formatter;
      Event evt = new Event(
     new[]
     {
        new KeyValuePair<String, Object>("Event.Message", "test"),
        new KeyValuePair<String, Object>("Event.Time", "1")
     }
      );
      // invalid formatter
      formatter = new Formatter() { Wrap = true };
      AssertException(() => formatter.FormatEventString(evt));
      formatter = new Formatter() { Wrap = true, Indent = 3, Hang = 3, Width = 5 };
      AssertException(() => formatter.FormatEventString(evt));
      // default format string
      formatter = new Formatter();
      Assert.IsNotNull(formatter.FormatEventString(evt));
      // custom format string
      formatter = new Formatter() { Format = "{0}{1}" };
      Assert.AreEqual(formatter.FormatEventString(evt), "test1\r\n");
      // indented event
      formatter = new Formatter() { Format = "{0}\r\n{1}", Indent = 2, Hang = 1 };
      Assert.AreEqual(formatter.FormatEventString(evt), "  test\r\n   1\r\n");
      // wrapped event
      formatter = new Formatter() { Format = "{0}{1}", Wrap = true, Width = 3 };
      Assert.AreEqual(formatter.FormatEventString(evt), "tes\r\nt1\r\n");
 }
Example #2
0
 public void TestComparison()
 {
     Event evt1, evt2, evt3;
      // empty comparison
      evt1 = new Event(new KeyValuePair<String, Object>[0]);
      evt2 = new Event(new KeyValuePair<String, Object>[0]);
      evt3 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.IsFalse(evt1.Equals(null));
      Assert.IsFalse(evt1.Equals(""));
      Assert.IsTrue(evt1.Equals(evt1));
      Assert.AreEqual(evt1.GetHashCode(), evt2.GetHashCode());
      Assert.IsTrue(evt1.Equals(evt2));
      Assert.IsFalse(evt1.Equals(evt3));
      // value comparison
      evt1 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1")
     }
      );
      evt2 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1")
     }
      );
      evt3 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value2")
     }
      );
      Assert.IsTrue(evt1.Equals(evt1));
      Assert.AreEqual(evt1.GetHashCode(), evt2.GetHashCode());
      Assert.IsTrue(evt1.Equals(evt2));
      Assert.IsFalse(evt1.Equals(evt3));
      evt3 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.IsTrue(evt1.Equals(evt1));
      Assert.AreEqual(evt1.GetHashCode(), evt2.GetHashCode());
      Assert.IsTrue(evt1.Equals(evt2));
      Assert.IsFalse(evt1.Equals(evt3));
      // count comparison
      evt1 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1")
     }
      );
      evt2 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1")
     }
      );
      evt3 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.IsTrue(evt1.Equals(evt1));
      Assert.AreEqual(evt1.GetHashCode(), evt2.GetHashCode());
      Assert.IsTrue(evt1.Equals(evt2));
      Assert.IsFalse(evt1.Equals(evt3));
      // list comparison
      evt1 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      evt2 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      evt3 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value2"),
        new KeyValuePair<String, Object>("test2", "value1")
     }
      );
      Assert.IsTrue(evt1.Equals(evt1));
      Assert.AreEqual(evt1.GetHashCode(), evt2.GetHashCode());
      Assert.IsTrue(evt1.Equals(evt2));
      Assert.IsFalse(evt1.Equals(evt3));
      // ordering comparison
      evt1 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test2", "value2"),
        new KeyValuePair<String, Object>("test1", "value1")
     }
      );
      evt2 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test2", "value2"),
        new KeyValuePair<String, Object>("test1", "value1")
     }
      );
      evt3 = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value2"),
        new KeyValuePair<String, Object>("test2", "value1")
     }
      );
      Assert.IsTrue(evt1.Equals(evt1));
      Assert.AreEqual(evt1.GetHashCode(), evt2.GetHashCode());
      Assert.IsTrue(evt1.Equals(evt2));
      Assert.IsFalse(evt1.Equals(evt3));
 }
Example #3
0
 public void TestProperties()
 {
     Event evt;
      // property map
      evt = new Event(new KeyValuePair<String, Object>[0]);
      Assert.AreEqual(evt[null], null);
      Assert.AreEqual(evt[""], null);
      Assert.AreEqual(evt[" "], null);
      Assert.AreEqual(evt["test"], null);
      evt = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.AreEqual(evt[null], null);
      Assert.AreEqual(evt[""], null);
      Assert.AreEqual(evt[" "], null);
      Assert.AreEqual(evt["test"], null);
      Assert.AreEqual(evt["test1"], "value1");
      Assert.AreEqual(evt["test2"], "value2");
      // property list
      evt = new Event(new KeyValuePair<String, Object>[0]);
      Assert.IsFalse(evt.Properties.Any());
      evt = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.AreEqual(evt.Properties.Count, 2);
      Assert.AreEqual(evt.Properties[0].Key, "test1");
      Assert.AreEqual(evt.Properties[0].Value, "value1");
      Assert.AreEqual(evt.Properties[1].Key, "test2");
      Assert.AreEqual(evt.Properties[1].Value, "value2");
      // property names
      evt = new Event(new KeyValuePair<String, Object>[0]);
      Assert.IsFalse(evt.Names.Any());
      evt = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.AreEqual(evt.Names.Count(), 2);
      Assert.AreEqual(evt.Names.First(), "test1");
      Assert.AreEqual(evt.Names.Skip(1).Single(), "test2");
      // property values
      evt = new Event(new KeyValuePair<String, Object>[0]);
      Assert.IsFalse(evt.Values.Any());
      evt = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.AreEqual(evt.Values.Count(), 2);
      Assert.AreEqual(evt.Values.First(), "value1");
      Assert.AreEqual(evt.Values.Skip(1).Single(), "value2");
 }
Example #4
0
 public void TestOperations()
 {
     Event evt;
      // string conversion
      evt = new Event(new KeyValuePair<String, Object>[0]);
      Assert.AreEqual(evt.ToString(), "");
      evt = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1")
     }
      );
      Assert.AreEqual(evt.ToString(), "test1: value1\r\n");
      evt = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.AreEqual(evt.ToString(), "test1: value1\r\ntest2: value2\r\n");
 }
Example #5
0
 public void TestConstruction()
 {
     Event evt;
      BinaryFormatter formatter = new BinaryFormatter();
      MemoryStream stream;
      // default construction
      evt = new Event(new KeyValuePair<String, Object>[0]);
      Assert.IsFalse(evt.Properties.Any());
      // invalid construction
      AssertException(
     () => new Event(new[]
        {
           new KeyValuePair<String, Object>("test1", "value1"),
           new KeyValuePair<String, Object>("test1", "value2")
        }
     )
      );
      // valid construction
      evt = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      Assert.IsTrue(evt.Properties.Any());
      // default serialized event
      stream = new MemoryStream();
      evt = new Event(new KeyValuePair<String, Object>[0]);
      formatter.Serialize(stream, evt);
      stream.Position = 0;
      evt = (Event)formatter.Deserialize(stream);
      Assert.IsFalse(evt.Properties.Any());
      // valid serialized event
      stream = new MemoryStream();
      evt = new Event(new[]
     {
        new KeyValuePair<String, Object>("test1", "value1"),
        new KeyValuePair<String, Object>("test2", "value2")
     }
      );
      formatter.Serialize(stream, evt);
      stream.Position = 0;
      evt = (Event)formatter.Deserialize(stream);
      Assert.AreEqual(evt.Properties.Count, 2);
      Assert.AreEqual(evt.Properties[0].Key, "test1");
      Assert.AreEqual(evt.Properties[0].Value, "value1");
      Assert.AreEqual(evt.Properties[1].Key, "test2");
      Assert.AreEqual(evt.Properties[1].Value, "value2");
 }
Example #6
0
 /// <summary>
 /// Formats a log event
 /// </summary>
 /// <param name="evt">
 /// The log event to format
 /// </param>
 /// <returns>
 /// The formatted event text
 /// </returns>
 public String FormatEventString(Event evt)
 {
     StringBuilder str = new StringBuilder();
      foreach (String line in FormatEvent(evt))
     str.AppendLine(line);
      return str.ToString();
 }
Example #7
0
 /// <summary>
 /// Formats a log event
 /// </summary>
 /// <param name="evt">
 /// The log event to format
 /// </param>
 /// <returns>
 /// The list of formatted text lines
 /// </returns>
 public IEnumerable<String> FormatEvent(Event evt)
 {
     // construct the format string, if not specified
      String format = this.Format;
      if (String.IsNullOrWhiteSpace(format))
     if (evt.Properties.Count == 1)
        format = "{0}";
     else
        format = String.Join("\r\n", evt.Names.Select((p, i) => String.Format("{0}: {{{1}}}", p, i)));
      return FormatMessage(String.Format(format, evt.Values.ToArray()));
 }