33using  Microsoft . Extensions . Hosting ; 
44using  Microsoft . Win32 ; 
55using  Serilog ; 
6+ using  ILogger  =  Serilog . ILogger ; 
67
78namespace  Coder . Desktop . Vpn . Service ; 
89
@@ -14,29 +15,22 @@ public static class Program
1415    // installer. 
1516#if ! DEBUG 
1617    private  const  string  ServiceName  =  "Coder Desktop" ; 
17-     private  const  string  ManagerConfigSection  =  "Manager "; 
18+     private  const  string  ConfigSubKey  =  @"SOFTWARE\Coder Desktop\VpnService "; 
1819#else
1920    // This value matches Create-Service.ps1. 
2021    private  const  string  ServiceName  =  "Coder Desktop (Debug)" ; 
21-     private  const  string  ManagerConfigSection  =  "DebugManager "; 
22+     private  const  string  ConfigSubKey  =  @"SOFTWARE\Coder Desktop\DebugVpnService "; 
2223#endif
2324
24-     private  const  string  ConsoleOutputTemplate  = 
25-         "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}" ; 
26- 
27-     private  const  string  FileOutputTemplate  = 
28-         "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}" ; 
25+     private  const  string  ManagerConfigSection  =  "Manager" ; 
2926
3027    private  static ILogger  MainLogger  =>  Log . ForContext ( "SourceContext" ,  "Coder.Desktop.Vpn.Service.Program" ) ; 
3128
32-     private  static LoggerConfiguration  BaseLogConfig  =>  new  LoggerConfiguration ( ) 
33-         . Enrich . FromLogContext ( ) 
34-         . MinimumLevel . Debug ( ) 
35-         . WriteTo . Console ( outputTemplate :  ConsoleOutputTemplate ) ; 
36- 
3729    public  static async  Task < int >  Main ( string [ ]  args ) 
3830    { 
39-         Log . Logger  =  BaseLogConfig . CreateLogger ( ) ; 
31+         // This logger will only be used until we load our full logging configuration and replace it. 
32+         Log . Logger  =  new  LoggerConfiguration ( ) . MinimumLevel . Debug ( ) . WriteTo . Console ( ) 
33+             . CreateLogger ( ) ; 
4034        MainLogger . Information ( "Application is starting" ) ; 
4135        try 
4236        { 
@@ -58,27 +52,26 @@ public static async Task<int> Main(string[] args)
5852    private  static async  Task  BuildAndRun ( string [ ]  args ) 
5953    { 
6054        var  builder  =  Host . CreateApplicationBuilder ( args ) ; 
55+         var  configBuilder  =  builder . Configuration  as  IConfigurationBuilder ; 
6156
6257        // Configuration sources 
6358        builder . Configuration . Sources . Clear ( ) ; 
64-         ( builder . Configuration  as  IConfigurationBuilder ) . Add ( 
65-             new  RegistryConfigurationSource ( Registry . LocalMachine ,  @"SOFTWARE\Coder Desktop" ) ) ; 
59+         AddDefaultConfig ( configBuilder ) ; 
60+         configBuilder . Add ( 
61+             new  RegistryConfigurationSource ( Registry . LocalMachine ,  ConfigSubKey ) ) ; 
6662        builder . Configuration . AddEnvironmentVariables ( "CODER_MANAGER_" ) ; 
6763        builder . Configuration . AddCommandLine ( args ) ; 
6864
6965        // Options types (these get registered as IOptions<T> singletons) 
7066        builder . Services . AddOptions < ManagerConfig > ( ) 
7167            . Bind ( builder . Configuration . GetSection ( ManagerConfigSection ) ) 
72-             . ValidateDataAnnotations ( ) 
73-             . PostConfigure ( config => 
74-             { 
75-                 Log . Logger  =  BaseLogConfig 
76-                     . WriteTo . File ( config . LogFileLocation ,  outputTemplate :  FileOutputTemplate ) 
77-                     . CreateLogger ( ) ; 
78-             } ) ; 
68+             . ValidateDataAnnotations ( ) ; 
7969
8070        // Logging 
81-         builder . Services . AddSerilog ( ) ; 
71+         builder . Services . AddSerilog ( ( _ ,  loggerConfig )  => 
72+         { 
73+             loggerConfig . ReadFrom . Configuration ( builder . Configuration ) ; 
74+         } ) ; 
8275
8376        // Singletons 
8477        builder . Services . AddSingleton < IDownloader ,  Downloader > ( ) ; 
@@ -101,6 +94,32 @@ private static async Task BuildAndRun(string[] args)
10194        builder . Services . AddHostedService < ManagerService > ( ) ; 
10295        builder . Services . AddHostedService < ManagerRpcService > ( ) ; 
10396
104-         await  builder . Build ( ) . RunAsync ( ) ; 
97+         var  host  =  builder . Build ( ) ; 
98+         Log . Logger  =  ( ILogger ) host . Services . GetService ( typeof ( ILogger ) ) ! ; 
99+         MainLogger . Information ( "Application is starting" ) ; 
100+ 
101+         await  host . RunAsync ( ) ; 
102+     } 
103+ 
104+     private  static void  AddDefaultConfig ( IConfigurationBuilder  builder ) 
105+     { 
106+         builder . AddInMemoryCollection ( new  Dictionary < string ,  string ? > 
107+         { 
108+             [ "Serilog:Using:0" ]  =  "Serilog.Sinks.File" , 
109+             [ "Serilog:Using:1" ]  =  "Serilog.Sinks.Console" , 
110+ 
111+             [ "Serilog:MinimumLevel" ]  =  "Information" , 
112+             [ "Serilog:Enrich:0" ]  =  "FromLogContext" , 
113+ 
114+             [ "Serilog:WriteTo:0:Name" ]  =  "File" , 
115+             [ "Serilog:WriteTo:0:Args:path" ]  =  @"C:\coder-desktop-service.log" , 
116+             [ "Serilog:WriteTo:0:Args:outputTemplate" ]  = 
117+                 "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}" , 
118+             [ "Serilog:WriteTo:0:Args:rollingInterval" ]  =  "Day" , 
119+ 
120+             [ "Serilog:WriteTo:1:Name" ]  =  "Console" , 
121+             [ "Serilog:WriteTo:1:Args:outputTemplate" ]  = 
122+                 "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext} - {Message:lj}{NewLine}{Exception}" , 
123+         } ) ; 
105124    } 
106125} 
0 commit comments