4

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

Rotaney
  • 247
  • 1
  • 3
  • 17

3 Answers3

4

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);
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
  • Thank you. This looks like very good. Unfortunately I have problems to use the code for me ($Log.Invoke(""): Can I say what log-level I need? ...) Have you found an interesting website where I can find more information? thank you. – Rotaney Oct 20 '10 at 14:04
  • OK. It doesn´t work in my code. So I have still two question to your code: What namespace is the "LogLevel". I have found: GemStone.GemFire.Cache on http://www.gemstone.com/docs/5.7.0/product/native_client/docs/sandcastle-CSharp/html/2A875BE3.htm! What do Action? I can only find a delegate-method. thank you. – Rotaney Oct 21 '10 at 07:20
  • @Rotaney: I don't know log4net, it was just an example of how to add a parameter to your function. An alternative could be having more than one function (Info, Warning, Error etc) and pass all those functions to your script. – Paolo Tedesco Oct 21 '10 at 07:50
  • Cool stuff, but unfortunately this does not work for PSRemoting, because of serialization. Is there still a way of doing this with PSRemoting? – Rookian Jul 10 '14 at 12:10
4

As an alternative you could reroute the streams and use the standard Write-Error, Write-Verbose, etc. CMDlets in your Script.

In your C# app attach methods to the streams events, like so:

PowerShell ps = PowerShell.Create();
// ... code to add your script, etc.
ps.Streams.Warning.DataAdded += new EventHandler<DataAddedEventArgs>(Warning_DataAdded);
// ... attach more streams for other log levels
ps.Invoke();

Create your methods like so:

static void Warning_DataAdded(object sender, DataAddedEventArgs e)
{
    PSDataCollection<WarningRecord> warningStream = (PSDataCollection<WarningRecord>)sender;
    log.Warn(warningStream[e.Index].Message);
}

This should write everything you output in your PowerShell Script via

Write-Warning "This is a warning message"

to the Warn level in log4net.

Hinek
  • 9,519
  • 12
  • 52
  • 74
0

I do not have details specific to log4net but it should be possible to load log4net like any other .Net component:

Accessing .NET components from Powershell Powershell Calling .NET Assembly that uses App.config

Community
  • 1
  • 1
Stefan Egli
  • 17,398
  • 3
  • 54
  • 75