77using Serilog . Extensions . Logging ;
88using SAPTeam . EasySign . CommandLine ;
99using SAPTeam . CommonTK ;
10+ using Spectre . Console ;
1011
1112namespace SAPTeam . EasySign . Cli
1213{
@@ -23,13 +24,13 @@ private static int Main(string[] args)
2324 . WriteTo . File (
2425 Path . Combine ( AppDirectory , "logs/log-.txt" ) ,
2526 rollingInterval : RollingInterval . Day ,
26- outputTemplate : "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Context}({ThreadId}) - {Message} {NewLine}{Exception}"
27+ outputTemplate : "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] {Context}({ThreadId}): {Message} {NewLine}{Exception}"
2728 )
2829 . MinimumLevel . Debug ( ) // Minimum log level
2930 . CreateLogger ( ) ;
3031
3132 Serilog . ILogger appLogger = Log . Logger . ForContext ( "Context" , "Main" ) ;
32- appLogger . Information ( "Starting EasySign CLI at {DateTime}" , DateTime . Now ) ;
33+ appLogger . Information ( "Starting EasySign CLI" ) ;
3334
3435 Microsoft . Extensions . Logging . ILogger bundleLogger = new SerilogLoggerFactory ( Log . Logger . ForContext ( "Context" , "Bundle" ) )
3536 . CreateLogger ( "CommandProvider" ) ;
@@ -39,28 +40,61 @@ private static int Main(string[] args)
3940
4041 if ( ! Directory . Exists ( AppDirectory ) )
4142 {
43+ appLogger . Debug ( "Creating application data directory at {AppDirectory}" , AppDirectory ) ;
4244 Directory . CreateDirectory ( AppDirectory ) ;
4345 }
4446
45- CommandProviderConfiguration config ;
47+ CommandProviderConfiguration ? config = null ;
4648 if ( File . Exists ( ConfigPath ) )
4749 {
48- using FileStream fs = File . OpenRead ( ConfigPath ) ;
49- config = JsonSerializer . Deserialize ( fs , typeof ( CommandProviderConfiguration ) , SourceGenerationConfigurationContext . Default ) as CommandProviderConfiguration ?? new CommandProviderConfiguration ( ) ;
50+ appLogger . Information ( "Loading configuration from {ConfigPath}" , ConfigPath ) ;
51+ FileStream fs = File . OpenRead ( ConfigPath ) ;
52+
53+ try
54+ {
55+ config = JsonSerializer . Deserialize ( fs , typeof ( CommandProviderConfiguration ) , SourceGenerationConfigurationContext . Default ) as CommandProviderConfiguration ?? new CommandProviderConfiguration ( ) ;
56+ fs . Dispose ( ) ;
57+ }
58+ catch
59+ {
60+ fs . Dispose ( ) ;
61+
62+ appLogger . Warning ( "Failed to load configuration from {ConfigPath}" , ConfigPath ) ;
63+ config = null ;
64+
65+ appLogger . Information ( "Creating backup of the old configuration file at {ConfigPath}.old" , ConfigPath + ".old" ) ;
66+ File . Copy ( ConfigPath , ConfigPath + ".old" , true ) ;
67+
68+ appLogger . Information ( "Deleting the broken configuration file at {ConfigPath}" , ConfigPath ) ;
69+ File . Delete ( ConfigPath ) ;
70+
71+ AnsiConsole . MarkupLine ( $ "[yellow]Failed to load configuration file. A backup has been created at { ConfigPath + ".old" } [/]") ;
72+ AnsiConsole . MarkupLine ( "[yellow]A new configuration file will be created with default values.[/]" ) ;
73+ }
5074 }
51- else
75+
76+ if ( config == null )
5277 {
78+ appLogger . Information ( "Loading default configuration" ) ;
5379 config = new CommandProviderConfiguration ( ) ;
5480 }
5581
82+ appLogger . Debug ( "Loading SAP Team trusted certificates" ) ;
5683 config . AddSAPTeamCertificates ( ) ;
84+
85+ appLogger . Debug ( "Initializing command provider" ) ;
5786 var cp = new BundleCommandProvider ( config , commandProviderLogger , bundleLogger ) ;
5887
88+ appLogger . Debug ( "Loading command line interface" ) ;
5989 RootCommand root = cp . GetRootCommand ( ) ;
90+
91+ appLogger . Information ( "Running command: {command}" , args ) ;
6092 int exitCode = root . Invoke ( args ) ;
93+ appLogger . Information ( "Command completed with exit code {exitCode}" , exitCode ) ;
6194
62- appLogger . Information ( "Shutting down EasySign CLI at {DateTime} with exit code {ExitCode}" , DateTime . Now , exitCode ) ;
95+ appLogger . Information ( "Shutting down EasySign CLI" ) ;
6396
97+ appLogger . Debug ( "Saving configuration to {ConfigPath}" , ConfigPath ) ;
6498 string data = JsonSerializer . Serialize ( config , config . GetType ( ) , SourceGenerationConfigurationContext . Default ) ;
6599
66100 if ( File . Exists ( ConfigPath ) )
@@ -72,7 +106,9 @@ private static int Main(string[] args)
72106 {
73107 fs . Write ( Encoding . UTF8 . GetBytes ( data ) ) ;
74108 }
109+ appLogger . Debug ( "Configuration saved to {ConfigPath}" , ConfigPath ) ;
75110
111+ appLogger . Debug ( "Application shutdown successfully completed" ) ;
76112 Log . CloseAndFlush ( ) ;
77113 return exitCode ;
78114 }
0 commit comments