88using Microsoft . Graph . Cli . Core . Authentication ;
99using Microsoft . Graph . Cli . Core . Commands . Authentication ;
1010using Microsoft . Graph . Cli . Core . Configuration ;
11+ using Microsoft . Graph . Cli . Core . Http ;
1112using Microsoft . Graph . Cli . Core . IO ;
1213using Microsoft . Kiota . Authentication . Azure ;
1314using Microsoft . Kiota . Cli . Commons . IO ;
1718using System . CommandLine . Hosting ;
1819using System . CommandLine . Parsing ;
1920using System . IO ;
21+ using System . Linq ;
2022using System . Reflection ;
2123using System . Threading . Tasks ;
2224using 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
0 commit comments