55using System . Linq ;
66using System . Xml ;
77using Confuser . Core ;
8+ using Confuser . Core . Diagnostics ;
89using Confuser . Core . Project ;
910using Microsoft . Extensions . Logging ;
1011using NDesk . Options ;
@@ -25,6 +26,8 @@ static int Main(string[] args) {
2526 bool noPause = false ;
2627 bool debug = false ;
2728 bool quiet = false ;
29+ bool dumpRequested = false ;
30+ string dumpPath = null ;
2831 int verbosity = 0 ;
2932 string outDir = null ;
3033 string snKeyPath = null ;
@@ -59,6 +62,9 @@ static int Main(string[] args) {
5962 } , {
6063 "q|quiet" , "only show warnings and errors." ,
6164 value => { quiet = ( value != null ) ; }
65+ } , {
66+ "dump:" , "write a diagnostic report (optionally to the given file)." ,
67+ value => { dumpRequested = true ; if ( ! string . IsNullOrEmpty ( value ) ) dumpPath = value ; }
6268 }
6369 } ;
6470
@@ -141,7 +147,7 @@ static int Main(string[] args) {
141147 parameters . Project = proj ;
142148 }
143149
144- int retVal = RunProject ( parameters , quiet , verbosity ) ;
150+ int retVal = RunProject ( parameters , quiet , verbosity , dumpRequested , dumpPath ) ;
145151
146152 if ( NeedPause ( ) && ! noPause ) {
147153 Console . WriteLine ( "Press any key to continue..." ) ;
@@ -203,7 +209,7 @@ static void LoadTemplateProject(string templatePath, ConfuserProject proj, List<
203209 templateModules . Add ( templateModule ) ;
204210 }
205211
206- static int RunProject ( ConfuserParameters parameters , bool quiet , int verbosity ) {
212+ static int RunProject ( ConfuserParameters parameters , bool quiet , int verbosity , bool dumpRequested , string dumpPath ) {
207213 var levelSwitch = quiet
208214 ? LogEventLevel . Warning
209215 : verbosity >= 3 ? LogEventLevel . Verbose
@@ -222,17 +228,44 @@ static int RunProject(ConfuserParameters parameters, bool quiet, int verbosity)
222228 var melLogger = loggerFactory . CreateLogger ( "ConfuserEx" ) ;
223229
224230 var progressReporter = new ConsoleProgressReporter ( ) ;
225- parameters . Logger = melLogger ;
226- parameters . ProgressReporter = progressReporter ;
231+
232+ // When a diagnostic report is requested, wrap both the logger and the progress reporter
233+ // with a collector so the report captures the full transcript, timing and outcome even
234+ // when the run fails.
235+ DiagnosticCollector collector = null ;
236+ if ( dumpRequested ) {
237+ collector = new DiagnosticCollector ( melLogger , progressReporter ) { Project = parameters . Project } ;
238+ parameters . Logger = collector ;
239+ parameters . ProgressReporter = collector ;
240+ }
241+ else {
242+ parameters . Logger = melLogger ;
243+ parameters . ProgressReporter = progressReporter ;
244+ }
227245
228246 if ( OperatingSystem . IsWindows ( ) )
229247 Console . Title = "ConfuserEx - Running..." ;
230248 ConfuserEngine . Run ( parameters ) . GetAwaiter ( ) . GetResult ( ) ;
231249
232250 Log . CloseAndFlush ( ) ;
251+
252+ if ( collector != null )
253+ WriteDiagnosticReport ( collector , dumpPath ) ;
254+
233255 return progressReporter . ReturnValue ;
234256 }
235257
258+ static void WriteDiagnosticReport ( DiagnosticCollector collector , string dumpPath ) {
259+ string path = string . IsNullOrEmpty ( dumpPath ) ? "confuser-diagnostic-report.md" : dumpPath ;
260+ try {
261+ File . WriteAllText ( path , collector . GenerateReport ( ) ) ;
262+ WriteLineWithColor ( ConsoleColor . Cyan , "Diagnostic report written to: " + Path . GetFullPath ( path ) ) ;
263+ }
264+ catch ( Exception ex ) {
265+ WriteLineWithColor ( ConsoleColor . Red , "Failed to write diagnostic report: " + ex . Message ) ;
266+ }
267+ }
268+
236269 static bool NeedPause ( ) {
237270 return Debugger . IsAttached || string . IsNullOrEmpty ( Environment . GetEnvironmentVariable ( "PROMPT" ) ) ;
238271 }
@@ -250,6 +283,7 @@ static void PrintUsage() {
250283 WriteLine ( " -snkeypass : specifies strong name key password." ) ;
251284 WriteLine ( " -v|verbose : increase verbosity (-v debug, -vv trace)." ) ;
252285 WriteLine ( " -q|quiet : only show warnings and errors." ) ;
286+ WriteLine ( " -dump : write a diagnostic report (-dump=<file> for a custom path)." ) ;
253287 }
254288
255289 static void WriteLineWithColor ( ConsoleColor color , string txt ) {
0 commit comments