Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 04ab286

Browse files
authored
Add debug option (#166)
Add debug option
1 parent 10abc3f commit 04ab286

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/Program.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Graph.Cli.Core.Authentication;
99
using Microsoft.Graph.Cli.Core.Commands.Authentication;
1010
using Microsoft.Graph.Cli.Core.Configuration;
11+
using Microsoft.Graph.Cli.Core.Http;
1112
using Microsoft.Graph.Cli.Core.IO;
1213
using Microsoft.Kiota.Authentication.Azure;
1314
using Microsoft.Kiota.Cli.Commons.IO;
@@ -17,6 +18,7 @@
1718
using System.CommandLine.Hosting;
1819
using System.CommandLine.Parsing;
1920
using System.IO;
21+
using System.Linq;
2022
using System.Reflection;
2123
using System.Threading.Tasks;
2224
using System.Collections.Generic;
@@ -50,7 +52,8 @@ static async Task<int> Main(string[] args)
5052
GraphServiceLibraryClientVersion = $"{assemblyVersion?.Major ?? 0}.{assemblyVersion?.Minor ?? 0}.{assemblyVersion?.Build ?? 0}",
5153
GraphServiceTargetVersion = "1.0"
5254
};
53-
using var httpClient = GraphCliClientFactory.GetDefaultClient(options);
55+
var loggingHandler = new LoggingHandler();
56+
using var httpClient = GraphCliClientFactory.GetDefaultClient(options, lowestPriorityMiddlewares: new[] { loggingHandler });
5457
var core = new HttpClientRequestAdapter(authProvider, httpClient: httpClient);
5558
var client = new GraphClient(core);
5659

@@ -66,6 +69,17 @@ static async Task<int> Main(string[] args)
6669
builder.AddMiddleware((invocation) =>
6770
{
6871
var host = invocation.GetHost();
72+
var isDebug = invocation.Parser.Configuration.RootCommand.Options.SingleOrDefault(static o => "debug".Equals(o.Name, StringComparison.Ordinal)) is Option<bool> debug ?
73+
invocation.ParseResult.GetValueForOption(debug) : false;
74+
if (isDebug == true)
75+
{
76+
loggingHandler.Logger = host.Services.GetService<ILogger<LoggingHandler>>();
77+
}
78+
else
79+
{
80+
loggingHandler.Logger = null;
81+
}
82+
6983
var outputFilter = host.Services.GetRequiredService<IOutputFilter>();
7084
var outputFormatterFactory = host.Services.GetRequiredService<IOutputFormatterFactory>();
7185
var pagingService = host.Services.GetRequiredService<IPagingService>();
@@ -89,7 +103,6 @@ static async Task<int> Main(string[] args)
89103
Console.ResetColor();
90104
Console.ForegroundColor = ConsoleColor.Red;
91105
context.Console.Error.WriteLine(ex.Message);
92-
context.Console.Error.WriteLine(ex.StackTrace);
93106
Console.ResetColor();
94107
break;
95108
}
@@ -110,6 +123,9 @@ static CommandLineBuilder BuildCommandLine(GraphClient client, IEnumerable<Comma
110123
rootCommand.AddCommand(command);
111124
}
112125

126+
var debug = new Option<bool>("--debug", "Turn on debug logging.");
127+
rootCommand.AddGlobalOption(debug);
128+
113129
return new CommandLineBuilder(rootCommand);
114130
}
115131

src/msgraph-cli.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
2424
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
2525
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
26-
<PackageReference Include="Microsoft.Graph.Cli.Core" Version="0.1.4-preview.1" />
26+
<PackageReference Include="Microsoft.Graph.Cli.Core" Version="0.1.4-preview.3" />
2727
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.0-preview.10" />
2828
<PackageReference Include="Microsoft.Kiota.Authentication.Azure" Version="1.0.0-preview.3" />
2929
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.0.0-preview.8" />

0 commit comments

Comments
 (0)