This repository has been archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDiagnostics.cs
158 lines (128 loc) · 5.58 KB
/
Diagnostics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Azure.Core.Diagnostics;
using Azure.Identity;
using Microsoft.Extensions.Options;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Graph.Community.Samples
{
public class Diagnostics
{
private readonly AzureAdSettings azureAdSettings;
private readonly SharePointSettings sharePointSettings;
public Diagnostics(
IOptions<AzureAdSettings> azureAdOptions,
IOptions<SharePointSettings> sharePointOptions)
{
this.azureAdSettings = azureAdOptions.Value;
this.sharePointSettings = sharePointOptions.Value;
}
public async Task Run()
{
////////////////////////////////////////
//
// Capture all diagnostic information
//
///////////////////////////////////////
// Start with an IHttpMessageLogger that will write to a StringBuilder
var logger = new StringBuilderHttpMessageLogger();
/*
* Could also use the Console if preferred...
*
* var logger = new ConsoleHttpMessageLogger();
*/
// MSAL provides logging via a callback on the client application.
// Write those entries to the same logger, prefixed with MSAL
//async void MSALLogging(LogLevel level, string message, bool containsPii)
//{
// await logger.WriteLine($"MSAL {level} {containsPii} {message}");
//}
// AzureSDK uses an EventSource to publish diagnostics in the token acquisition.
// Setup a listener to monitor logged events.
//using AzureEventSourceListener azListener = AzureEventSourceListener.CreateConsoleLogger();
var azListener = new AzureEventSourceListener(async (args, message) =>
{
// create a dictionary of the properties of the args object
var properties = args.PayloadNames
.Zip(args.Payload, (string k, object v) => new { Key = k, Value = v })
.ToDictionary(x => x.Key, x => x.Value.ToString());
// log the message and payload, prefixed with COMM
var traceMessage = string.Format(args.Message, args.Payload.ToArray());
await logger.WriteLine($"AZ {traceMessage}");
}, System.Diagnostics.Tracing.EventLevel.LogAlways);
// GraphCommunity uses an EventSource to publish diagnostics in the handler.
// This follows the pattern used by the Azure SDK.
var listener = new Community.Diagnostics.GraphCommunityEventSourceListener(async (args, message) =>
{
if (args.EventSource.Name.StartsWith("Graph-Community"))
{
// create a dictionary of the properties of the args object
var properties = args.PayloadNames
.Zip(args.Payload, (string k, object v) => new { Key = k, Value = v })
.ToDictionary(x => x.Key, x => x.Value.ToString());
// log the message and payload, prefixed with COMM
var traceMessage = string.Format(args.Message, args.Payload.ToArray());
await logger.WriteLine($"COMM {traceMessage}");
}
}, System.Diagnostics.Tracing.EventLevel.LogAlways);
//////////////////////
//
// TokenCredential
//
//////////////////////
var credential = new ChainedTokenCredential(
new SharedTokenCacheCredential(new SharedTokenCacheCredentialOptions() { TenantId = azureAdSettings.TenantId, ClientId = azureAdSettings.ClientId }),
new VisualStudioCredential(new VisualStudioCredentialOptions { TenantId = azureAdSettings.TenantId }),
new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions { TenantId = azureAdSettings.TenantId, ClientId = azureAdSettings.ClientId })
);
////////////////////////////////////////////////////////////
//
// Graph Client with Logger and SharePoint service handler
//
////////////////////////////////////////////////////////////
// Configure our client
CommunityGraphClientOptions clientOptions = new CommunityGraphClientOptions()
{
UserAgent = "DiagnosticsSample"
};
var graphServiceClient = CommunityGraphClientFactory.Create(clientOptions, logger, credential);
///////////////////////////////////////
//
// Setup is complete, run the sample
//
///////////////////////////////////////
try
{
var scopes = new string[] { $"https://{sharePointSettings.Hostname}/AllSites.FullControl" };
var WebUrl = $"https://{sharePointSettings.Hostname}{sharePointSettings.SiteCollectionUrl}";
var appTiles = await graphServiceClient
.SharePointAPI(WebUrl)
.Web
.AppTiles
.Request()
.WithScopes(scopes)
.GetAsync();
Console.WriteLine($"Tile count: {appTiles.Count}");
var me = await graphServiceClient
.Me
.Request()
.WithScopes(new string[] { "https://graph.microsoft.com/User.Read" })
.GetAsync();
Console.WriteLine($"Me.DisplayName: {me.DisplayName}");
}
catch (Exception ex)
{
await logger.WriteLine("");
await logger.WriteLine("================== Exception caught ==================");
await logger.WriteLine(ex.ToString());
}
Console.WriteLine("Press enter to show log");
Console.ReadLine();
Console.WriteLine();
var log = logger.GetLog();
Console.WriteLine(log);
}
}
}