C# Build with csc, Execute from Commandline - Getting Exceptions

58 Views Asked by At

I have built a dll with csc.exe by using:

csc.exe "/target:library" "/out:$($path).dll" "$($path).cs"

From the powershell console I run it with:

powershell.exe -command {[byte[]]$bytes = [System.IO.File]::ReadAllBytes("$($args[0]).dll"); $assembly = [Reflection.Assembly]::Load($bytes); [ns.main]::Main();} -args $path

However when an exception occurs it just quits and prints nothing. I have to use try / catch in the code to trace down where it came from.

What do I have to change to get exceptions printed before it quits?

1

There are 1 best solutions below

0
aydunno On

In the code I tried the event "UnhandledExceptionEventHandler" with AppDomain but that didnt work. You can however get the exception from the event log and $LASTEXITCODE is also set accordingly.

So I added this after the build and subsequent execution commands:

if ($LASTEXITCODE -ne 0)
{
  Get-EventLog -logname Application -EntryType Error -source ".NET Runtime" - 
  Message *powershell* -newest 1| select-object -property timegenerated,message | 
   format-list
}