I have written a program with C#, that creates a logfile and fills this by using log4net. This program starts powershell-scripts. The scripts should use log4net, too. I want to monitor what the scripts log to log4net while they are running and write this information in my logfile.
Have you an idea how I do this or where I get information/help to my problem?
thanks
You could define some functions and pass them to your script as variables:
    static void Log(string message) {
        // log4net code here...
    }
    void ExecuteScript() {
        // create the runspace configuration
        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
        // create the runspace, open it and add variables for the script
        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
        runspace.Open();
        // pass the Log function as a variable
        runspace.SessionStateProxy.SetVariable("Log", (Action<string>)Log);
        // etc...
Then you can invoke the function from the script like this:
$Log.Invoke("test")
EDIT: to add a logging level, you should do something like
    static void Log(LogLevel level,string message) {
        // log4net code here...
    }
     void ExecuteScript() {
        // ...
        runspace.SessionStateProxy.SetVariable("Log", (Action<LogLevel,string>)Log);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With