dotnetco.de

Log all assemblies used

Sometimes it’s quite helpful to know which assembly is currently used by an application.  Microsoft has an example in their MSDN-Article explaining AssemblyName.Version Property. So this article is more a reminder because it’s quite helpful on application initialization to log the assemblies sometimes. But logging should not be done too early, because assemblies are not available as long as they are not used so it would be better to add it at the end of the initialization.

The logger is log4net but could of course easily be changed.
Update 07. January 2014: Yesterday evening several Windows Patches have been deployed on our server. Since then one of our vb.net application did not work anymore. It worked fine with .net 4.0 before but now crashes with the following exception: The invoked member is not supported in a dynamic assembly. The reason is found here: http://bloggingabout.net/blogs/vagif/archive/2010/07/02/net-4-0-and-notsupportedexception-complaining-about-dynamic-assemblies.aspx  Strange because the application ran fine so far, but installing patch described in Microsofts KB 2858725 stopped the application from working. So please find below the updated version.

Private Sub LogAllAssemblies(ByVal log As log4net.ILog)
 Dim MyAssemblies As System.Reflection.Assembly()
 Dim i As Integer
 MyAssemblies = System.Threading.Thread.GetDomain().GetAssemblies()
 log.Info("Current application uses " & MyAssemblies.Length.ToString & " assemblies:")
 For i = 0 To MyAssemblies.GetUpperBound(0)
   Try
     If MyAssemblies(i).GetType().FullName = "System.Reflection.Emit.InternalAssemblyBuilder" Then
       log.Debug("Reflection is not supported by assembly Nr " & (i + 1).ToString & " so could not log all details for it.")
       log.Debug((i + 1).ToString & ". " & MyAssemblies(i).GetName().Name & " (Location not supported) - " & _
         "From GAC: " & MyAssemblies(i).GlobalAssemblyCache.ToString & _
         " - Image Runtime: " & MyAssemblies(i).ImageRuntimeVersion & _
         " - Version: " & MyAssemblies(i).GetName().Version.ToString & _
         " - Full name: " & MyAssemblies(i).FullName)
     Else
       log.Debug((i + 1).ToString & ". " & MyAssemblies(i).GetName().Name & " (" & MyAssemblies(i).Location & ") - " & _
         "From GAC: " & MyAssemblies(i).GlobalAssemblyCache.ToString & _
         " - Image Runtime: " & MyAssemblies(i).ImageRuntimeVersion & _
         " - Version: " & MyAssemblies(i).GetName().Version.ToString & _
         " - Full name: " & MyAssemblies(i).FullName)
     End If
   Catch ex As Exception
     log.Debug("Could not log assembly Nr " & (i + 1).ToString & ": " & ex.Message)
   End Try
 Next
End Sub

 

Leave a Comment