Skip to content

Commit 5cf2888

Browse files
Add ALC logging
1 parent 1b4d236 commit 5cf2888

1 file changed

Lines changed: 66 additions & 6 deletions

File tree

src/dsc/psresourceget.ps1

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,76 @@ function ConvertInputToPSResource(
276276
trap {
277277
# Write-Trace -Level Error -message $_.Exception.Message
278278
# exit 1
279+
Get-LoadedAssembliesByALC | Select-Object -Property ALCName, AssemblyName, Location | ForEach-Object {
280+
Write-Trace -message "ALC: $($_.ALCName) - Assembly: $($_.AssemblyName) - Location: $($_.Location)" -level trace
281+
}
282+
283+
exit 1
284+
}
285+
286+
287+
function Get-LoadedAssembliesByALC {
288+
[CmdletBinding()]
289+
param(
290+
# Optional filter: only show assemblies whose simple name matches this regex
291+
[string] $NameMatch,
292+
293+
# Optional: show only AssemblyLoadContexts whose name matches this regex
294+
[string] $ALCMatch
295+
)
296+
297+
# AssemblyLoadContext is in System.Runtime.Loader
298+
$alcType = [System.Runtime.Loader.AssemblyLoadContext]
299+
300+
# Enumerate all ALCs currently alive
301+
$alcs = $alcType::All
302+
303+
$rows = foreach ($alc in $alcs) {
279304

280-
$e = $_.Exception
305+
if ($ALCMatch -and ($alc.Name -notmatch $ALCMatch)) { continue }
281306

282-
while ($e) {
283-
"-----"
284-
$e.GetType().FullName
285-
$e.Message
286-
$e = $e.InnerException
307+
foreach ($asm in $alc.Assemblies) {
308+
309+
$an = $asm.GetName()
310+
311+
if ($NameMatch -and ($an.Name -notmatch $NameMatch)) { continue }
312+
313+
# Some assemblies are dynamic/in-memory and have no Location
314+
$location = $null
315+
$isDynamic = $false
316+
try {
317+
$isDynamic = $asm.IsDynamic
318+
if (-not $isDynamic) { $location = $asm.Location }
319+
} catch {
320+
# Some runtime assemblies can still throw on Location; ignore safely
321+
$location = $null
322+
}
323+
324+
$pkt = $null
325+
try {
326+
$bytes = $an.GetPublicKeyToken()
327+
if ($bytes -and $bytes.Length -gt 0) {
328+
$pkt = ($bytes | ForEach-Object { $_.ToString("x2") }) -join ""
329+
}
330+
} catch { $pkt = $null }
331+
332+
[pscustomobject]@{
333+
ALCName = if ($alc.Name) { $alc.Name } else { "<unnamed>" }
334+
IsDefaultALC = ($alc -eq [System.Runtime.Loader.AssemblyLoadContext]::Default)
335+
IsCollectible = $alc.IsCollectible
336+
AssemblyName = $an.Name
337+
Version = $an.Version.ToString()
338+
Culture = if ($an.CultureInfo) { $an.CultureInfo.Name } else { "" }
339+
PublicKeyToken = $pkt
340+
IsDynamic = $isDynamic
341+
Location = if ($location) { $location } else { if ($isDynamic) { "<dynamic>" } else { "" } }
342+
FullName = $asm.FullName
343+
}
344+
}
287345
}
288346

347+
# Sort for readability: by ALC then assembly name
348+
$rows | Sort-Object ALCName, AssemblyName, Version
289349
}
290350

291351
function GetPSResourceList {

0 commit comments

Comments
 (0)