Accessing C# dll's methods with Powershell.
-
Monday, April 12, 2010 1:33 PM
I wrote a DLL in C# for logging to text file. It has static functions for logging.I wrote a sample application in C# using this dll and it worked fine without ANY EXCEPTIONS.
Now I want to use it in powershell, so I wrote following script which loads dll and use function in similar manner as I was using it in C# sample application. But here I see exceptions. Please see output below. I could not figure out why these are there?. Adding to my wonder, even if these exceptions in O/P are there my dll worked perfectly fine and wrote all log to expected log file. I just want to get rid of these exception.
$ep = get-executionpolicy if ($ep -eq ‘unrestricted’) { write-warning “: harness running in unrestricted mode`n” } [Reflection.Assembly]::LoadFile("logger.dll"); [loggers.masterlog]::LoggerInitialize("test.txt","Log.xml") [loggers.masterlog]::LogTestStart("Start of test")| out-null [loggers.masterlog]::LogDebugL1("Debug 1")| out-null [loggers.masterlog]::LogPass("Test case Passed")| out-null [loggers.masterlog]::close()| out-nullBelow BOLD messages are from exception handlers in C# dll.
PS lib> .\sampleLog.PS1
WARNING: : harness running in unrestricted modeGAC Version Location
--- ------- --------
False v2.0.50727 logger.dllError While creating log file :
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShar
e share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at Loggers.TextLogStream.LogInitialize(String logFileName)
0
Error in logging to text log stream
at System.Threading.Monitor.Enter(Object obj)
at Loggers.TextLogStream.Log(logLevels level, String Message)
Error in logging to text log stream
at System.Threading.Monitor.Enter(Object obj)
at Loggers.TextLogStream.Log(logLevels level, String Message)
Error in logging to text log stream
at System.Threading.Monitor.Enter(Object obj)
at Loggers.TextLogStream.Log(logLevels level, String Message)
Vishal Jain, MSFT
All Replies
-
Monday, April 12, 2010 11:43 PMModerator
I lack developement-side skills, but have asked some of my Posh-dev friends for their opinion (no response yet).
Is it acceptable to simply trap the errors or you would like to find out the root cause?
Out of curiosity, assuming you're using V2, do you get the same error in the PowerShell console and the ISE?
-
Tuesday, April 13, 2010 3:22 AM
Your path's restricted, obviously -- you can't create a log file there ;)
PowerShell starts, by default, in C:\Windows\System32\WindowsPowerShell\v1.0
When you change directories, it never updates the Environment.CurrentDirectory
Your code just passes a simple file name, and, I assume, creates the log file in the current working directory?
So anyway, try passing a full file path to LoggerInitialize, or set [Environment]::CurrentDirectory = $pwd once in awhile (I set it in my prompt function), or change the initialize method to default to a different folder.
As a side note, it might be easier to just use Log4Net ;) http://poshcode.org/1752
- Marked As Answer by Marco ShawModerator Tuesday, April 13, 2010 11:58 AM

