Skip to content

Commit 645d140

Browse files
committed
Added json output
1 parent 909fcc4 commit 645d140

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed

ClientLib/ClientLib.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@
4444
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
4545
</PropertyGroup>
4646
<ItemGroup>
47-
<Reference Include="Amqp.Net, Version=2.0.0.0, Culture=neutral, PublicKeyToken=905a7b1e6458e0c3, processorArchitecture=MSIL">
48-
<HintPath>..\packages\AMQPNetLite.2.0.0\lib\net45\Amqp.Net.dll</HintPath>
47+
<Reference Include="Amqp.Net, Version=2.1.0.0, Culture=neutral, PublicKeyToken=905a7b1e6458e0c3, processorArchitecture=MSIL">
48+
<HintPath>..\packages\AMQPNetLite.2.1.1\lib\net45\Amqp.Net.dll</HintPath>
4949
</Reference>
5050
<Reference Include="NDesk.Options, Version=0.2.1.0, Culture=neutral, processorArchitecture=MSIL">
5151
<HintPath>..\packages\NDesk.Options.0.2.1\lib\NDesk.Options.dll</HintPath>
5252
<Private>True</Private>
5353
</Reference>
54+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
55+
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
56+
</Reference>
5457
<Reference Include="System" />
5558
<Reference Include="System.Core" />
5659
<Reference Include="System.Transactions" />

ClientLib/Formatter.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.Collections.ObjectModel;
19+
using System.Collections.ObjectModel;
2020
using System.Text;
2121

2222
using Amqp;
2323
using Amqp.Types;
24-
24+
using Newtonsoft.Json;
25+
2526
namespace ClientLib
2627
{
2728
/// <summary>
@@ -112,6 +113,36 @@ public static void PrintMessageAsInterop(Message msg)
112113
Console.WriteLine(FormatMap(msgDict));
113114
}
114115

116+
/// <summary>
117+
/// Print message as python dict (keys are named by AMQP standard)
118+
/// </summary>
119+
/// <param name="msg">message object</param>
120+
public static void PrintMessageAsJson(Message msg)
121+
{
122+
Dictionary<string, object> msgDict = new Dictionary<string, object>();
123+
msgDict.Add("durable", msg.Header.Durable);
124+
msgDict.Add("ttl", msg.Header.Ttl);
125+
msgDict.Add("delivery-count", msg.Header.DeliveryCount);
126+
msgDict.Add("priority", (uint)msg.Header.Priority);
127+
msgDict.Add("first-aquirer", msg.Header.FirstAcquirer);
128+
msgDict.Add("id", RemoveIDPrefix(msg.Properties.MessageId));
129+
msgDict.Add("address", msg.Properties.To);
130+
msgDict.Add("reply-to", msg.Properties.ReplyTo);
131+
msgDict.Add("subject", msg.Properties.Subject);
132+
msgDict.Add("creation-time", msg.Properties.CreationTime);
133+
msgDict.Add("absolute-expiry-time", msg.Properties.AbsoluteExpiryTime);
134+
msgDict.Add("content-encoding", msg.Properties.ContentEncoding);
135+
msgDict.Add("content-type", msg.Properties.ContentType);
136+
msgDict.Add("correlation-id", RemoveIDPrefix(msg.Properties.CorrelationId));
137+
msgDict.Add("user-id", msg.Properties.UserId);
138+
msgDict.Add("group-id", msg.Properties.GroupId);
139+
msgDict.Add("group-sequence", msg.Properties.GroupSequence);
140+
msgDict.Add("reply-to-group-id", msg.Properties.ReplyToGroupId);
141+
msgDict.Add("content", msg.Body);
142+
msgDict.Add("properties", msg.ApplicationProperties);
143+
Console.WriteLine(JsonConvert.SerializeObject(msgDict));
144+
}
145+
115146
/// <summary>
116147
/// Print statistic info
117148
/// </summary>
@@ -539,6 +570,10 @@ public static void LogMessage(Message msg, SenderReceiverOptions options)
539570
{
540571
Formatter.PrintMessageAsInterop(msg);
541572
}
573+
else if (options.LogMsgs.Equals("json"))
574+
{
575+
Formatter.PrintMessageAsJson(msg);
576+
}
542577
}
543578

544579

ClientLib/packages.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="AMQPNetLite" version="2.0.0" targetFramework="net45" />
3+
<package id="AMQPNetLite" version="2.1.1" targetFramework="net45" />
44
<package id="NDesk.Options" version="0.2.1" targetFramework="net45" />
5+
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
56
</packages>

ClientLibNetCore/ClientLibNetCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="amqpnetlite" Version="2.0.0" />
8+
<PackageReference Include="amqpnetlite" Version="2.1.1" />
9+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
910
</ItemGroup>
1011

1112
</Project>

ClientLibNetCore/Formatter.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19-
using System.Collections.ObjectModel;
19+
using System.Collections.ObjectModel;
2020
using System.Text;
2121

2222
using Amqp;
2323
using Amqp.Types;
24-
24+
using Newtonsoft.Json;
25+
2526
namespace ClientLib
2627
{
2728
/// <summary>
@@ -112,6 +113,36 @@ public static void PrintMessageAsInterop(Message msg)
112113
Console.WriteLine(FormatMap(msgDict));
113114
}
114115

116+
/// <summary>
117+
/// Print message as python dict (keys are named by AMQP standard)
118+
/// </summary>
119+
/// <param name="msg">message object</param>
120+
public static void PrintMessageAsJson(Message msg)
121+
{
122+
Dictionary<string, object> msgDict = new Dictionary<string, object>();
123+
msgDict.Add("durable", msg.Header.Durable);
124+
msgDict.Add("ttl", msg.Header.Ttl);
125+
msgDict.Add("delivery-count", msg.Header.DeliveryCount);
126+
msgDict.Add("priority", (uint)msg.Header.Priority);
127+
msgDict.Add("first-aquirer", msg.Header.FirstAcquirer);
128+
msgDict.Add("id", RemoveIDPrefix(msg.Properties.MessageId));
129+
msgDict.Add("address", msg.Properties.To);
130+
msgDict.Add("reply-to", msg.Properties.ReplyTo);
131+
msgDict.Add("subject", msg.Properties.Subject);
132+
msgDict.Add("creation-time", msg.Properties.CreationTime);
133+
msgDict.Add("absolute-expiry-time", msg.Properties.AbsoluteExpiryTime);
134+
msgDict.Add("content-encoding", msg.Properties.ContentEncoding);
135+
msgDict.Add("content-type", msg.Properties.ContentType);
136+
msgDict.Add("correlation-id", RemoveIDPrefix(msg.Properties.CorrelationId));
137+
msgDict.Add("user-id", msg.Properties.UserId);
138+
msgDict.Add("group-id", msg.Properties.GroupId);
139+
msgDict.Add("group-sequence", msg.Properties.GroupSequence);
140+
msgDict.Add("reply-to-group-id", msg.Properties.ReplyToGroupId);
141+
msgDict.Add("content", msg.Body);
142+
msgDict.Add("properties", msg.ApplicationProperties);
143+
Console.WriteLine(JsonConvert.SerializeObject(msgDict));
144+
}
145+
115146
/// <summary>
116147
/// Print statistic info
117148
/// </summary>
@@ -539,6 +570,10 @@ public static void LogMessage(Message msg, SenderReceiverOptions options)
539570
{
540571
Formatter.PrintMessageAsInterop(msg);
541572
}
573+
else if (options.LogMsgs.Equals("json"))
574+
{
575+
Formatter.PrintMessageAsJson(msg);
576+
}
542577
}
543578

544579

0 commit comments

Comments
 (0)