11using Microsoft . AspNetCore ;
22using Microsoft . AspNetCore . Hosting ;
33using Microsoft . Extensions . Configuration ;
4- using Microsoft . Extensions . Logging ;
54using Serilog ;
65using System ;
6+ using System . Collections . Generic ;
77using System . IO ;
8+ using System . Linq ;
9+ using System . Reflection ;
810
911namespace WebStatus
1012{
1113 public class Program
1214 {
13- public static readonly string Namespace = typeof ( Program ) . Namespace ;
1415 public static readonly string AppName = Namespace ;
16+ public static readonly string Namespace = typeof ( Program ) . Namespace ;
1517
1618 public static int Main ( string [ ] args )
1719 {
@@ -24,6 +26,8 @@ public static int Main(string[] args)
2426 Log . Information ( "Configuring web host ({ApplicationContext})..." , AppName ) ;
2527 var host = BuildWebHost ( configuration , args ) ;
2628
29+ LogPackagesVersionInfo ( ) ;
30+
2731 Log . Information ( "Starting web host ({ApplicationContext})..." , AppName ) ;
2832 host . Run ( ) ;
2933
@@ -83,5 +87,39 @@ private static IConfiguration GetConfiguration()
8387
8488 return builder . Build ( ) ;
8589 }
90+
91+ private static string GetVersion ( Assembly assembly )
92+ {
93+ try
94+ {
95+ return $ "{ assembly . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) ? . Version } ({ assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion . Split ( ) [ 0 ] } )";
96+ }
97+ catch
98+ {
99+ return string . Empty ;
100+ }
101+ }
102+
103+ private static void LogPackagesVersionInfo ( )
104+ {
105+ var assemblies = new List < Assembly > ( ) ;
106+
107+ foreach ( var dependencyName in typeof ( Program ) . Assembly . GetReferencedAssemblies ( ) )
108+ {
109+ try
110+ {
111+ // Try to load the referenced assembly...
112+ assemblies . Add ( Assembly . Load ( dependencyName ) ) ;
113+ }
114+ catch
115+ {
116+ // Failed to load assembly. Skip it.
117+ }
118+ }
119+
120+ var versionList = assemblies . Select ( a => $ "-{ a . GetName ( ) . Name } - { GetVersion ( a ) } ") . OrderBy ( value => value ) ;
121+
122+ Log . Logger . ForContext ( "PackageVersions" , string . Join ( "\n " , versionList ) ) . Information ( "Package versions ({ApplicationContext})" , AppName ) ;
123+ }
86124 }
87125}
0 commit comments