public void Test() { BitcoinMessageFormatter formatter = new BitcoinMessageFormatter("\t"); Assert.That(formatter.Format(null), Is.EqualTo("\t<null>")); Assert.That(formatter.Format(new FakeMessage()), Is.StringStarting("\tcommand:\n\t\tfake\n\t<Formatting is not supported")); Assert.That( formatter.Format( new InvMessage( new InventoryVector[] { new InventoryVector(InventoryVectorType.MsgBlock, new byte[32]) }) ), Is.EqualTo( "\tcommand:" + "\n\t\tinv" + "\n\tinventory items [1 item]:" + "\n\t\tMsgBlock\t" + "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-" + "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00" )); }
private string FormatForLog(BitcoinMessage message) { StringBuilder sb = new StringBuilder(); BitcoinMessageFormatter formatter = new BitcoinMessageFormatter("\t"); sb.AppendFormat("{0} [{1} byte(s)]", message.Command, message.Payload.Length); try { IBitcoinMessage parsedMessage = BitcoinMessageParser.Parse(message); if (parsedMessage != null) { sb.Append("\n"); sb.Append(formatter.Format(parsedMessage)); } } catch (Exception e) { sb.AppendFormat("\n\tUnable to parse message: {0}", e.Message); } return sb.ToString(); }