Skip to content

Commit b2ac568

Browse files
AMSoftwareNLArjan Meskers
andauthored
Log Dataverse Organization Request and Response for Debug (#10)
* MSALIdentityLogger and ServiceClientLogger * Log serialized request and response for debug --------- Co-authored-by: Arjan Meskers <arjanmeskers@outlook.com>
1 parent 3689db3 commit b2ac568

2 files changed

Lines changed: 43 additions & 12 deletions

File tree

src/AMSoftware.Dataverse.PowerShell/CmdletBase.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ protected override void BeginProcessing()
4444
WriteVerboseWithTimestamp(string.Format("{0} begin processing with ParameterSet '{1}'.", GetType().Name, ParameterSetName));
4545
}
4646

47-
MSALIdentityLogger.Instance.AddContext((entry) =>
48-
{
49-
LogMessages.Enqueue(entry);
50-
});
51-
ServiceClientLogger.Instance.AddContext((entry) =>
52-
{
53-
LogMessages.Enqueue(entry);
54-
});
47+
SetupLogIntercept();
5548

5649
base.BeginProcessing();
5750
}
@@ -83,16 +76,32 @@ protected override void ProcessRecord()
8376

8477
protected override void EndProcessing()
8578
{
86-
MSALIdentityLogger.Instance.RemoveContext();
87-
ServiceClientLogger.Instance.RemoveContext();
88-
79+
TeardownLogIntercept();
8980
FlushLogMessages();
9081

9182
WriteVerboseWithTimestamp(string.Format("{0} end processing.", GetType().Name));
9283

9384
base.EndProcessing();
9485
}
9586

87+
private void SetupLogIntercept()
88+
{
89+
MSALIdentityLogger.Instance.AddContext((entry) =>
90+
{
91+
LogMessages.Enqueue(entry);
92+
});
93+
ServiceClientLogger.Instance.AddContext((entry) =>
94+
{
95+
LogMessages.Enqueue(entry);
96+
});
97+
}
98+
99+
private static void TeardownLogIntercept()
100+
{
101+
MSALIdentityLogger.Instance.RemoveContext();
102+
ServiceClientLogger.Instance.RemoveContext();
103+
}
104+
96105
private void FlushLogMessages()
97106
{
98107
while (LogMessages.TryDequeue(out PSLogEntry entry))

src/AMSoftware.Dataverse.PowerShell/RequestCmdletBase.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ You should have received a copy of the GNU General Public License
1717
*/
1818
using Microsoft.Xrm.Sdk;
1919
using System.Management.Automation;
20+
using System.Runtime.Serialization;
21+
using System.Text;
22+
using System.Xml;
2023

2124
namespace AMSoftware.Dataverse.PowerShell
2225
{
@@ -49,18 +52,37 @@ public abstract class RequestCmdletBase : CmdletBase
4952
protected virtual TResponse ExecuteOrganizationRequest<TResponse>(OrganizationRequest request) where TResponse : OrganizationResponse
5053
{
5154
UseOptionalParameters(request);
55+
LogForDebug(request);
5256

5357
var response = (TResponse)Session.Current.Client.ExecuteOrganizationRequest(
5458
request, MyInvocation.MyCommand.Name);
5559

56-
if (response == null)
60+
if (response == null && Session.Current.Client.LastException != null)
5761
{
5862
throw Session.Current.Client.LastException;
5963
}
6064

65+
LogForDebug(response);
66+
6167
return response;
6268
}
6369

70+
private void LogForDebug<T>(T graph)
71+
{
72+
var serializer = new DataContractSerializer(typeof(T));
73+
var debugStringBuilder = new StringBuilder();
74+
75+
using (var xmlWriter = XmlWriter.Create(debugStringBuilder, new XmlWriterSettings()
76+
{
77+
Indent = true
78+
})) {
79+
serializer.WriteObject(xmlWriter, graph);
80+
}
81+
82+
WriteDebugWithTimestamp(graph.GetType().Name);
83+
WriteDebug(debugStringBuilder.ToString());
84+
}
85+
6486
private void UseOptionalParameters(OrganizationRequest request)
6587
{
6688
if (request == null) return;

0 commit comments

Comments
 (0)