【问题标题】:Logging from powershell-script to csharp-program (log4net - logfile)从 powershell-script 记录到 csharp-program (log4net - logfile)
【发布时间】:2011-04-27 23:52:48
【问题描述】:

我用 C# 编写了一个程序,它创建一个日志文件并使用 log4net 填充它。该程序启动 powershell 脚本。脚本也应该使用 log4net。我想监控脚本在运行时记录到 log4net 的内容,并将这些信息写入我的日志文件中。

您知道我是如何做到这一点的,或者我从哪里获得信息/帮助解决我的问题?

谢谢

【问题讨论】:

    标签: c# .net powershell log4net


    【解决方案1】:

    作为替代方案,您可以重新路由流并在脚本中使用标准的 Write-Error、Write-Verbose 等 CMDlet。

    在您的 C# 应用程序中,将方法附加到流事件,如下所示:

    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();
    

    像这样创建你的方法:

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

    这应该将您输出的所有内容写入您的 PowerShell 脚本中

    Write-Warning "This is a warning message"
    

    到 log4net 中的警告级别。

    【讨论】:

      【解决方案2】:

      您可以定义一些函数并将它们作为变量传递给您的脚本:

          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...
      

      然后你可以像这样从脚本中调用函数:

      $Log.Invoke("test")
      

      编辑:要添加日志级别,您应该执行类似的操作

          static void Log(LogLevel level,string message) {
              // log4net code here...
          }
      
           void ExecuteScript() {
              // ...
              runspace.SessionStateProxy.SetVariable("Log", (Action<LogLevel,string>)Log);
      

      【讨论】:

      • 谢谢。这看起来非常好。不幸的是,我在使用代码时遇到了问题($Log.Invoke(""): 我能说一下我需要什么日志级别吗?...)您是否找到了一个有趣的网站,我可以在其中找到更多信息?谢谢。
      • 好的。它在我的代码中不起作用。所以我对你的代码还有两个问题:“LogLevel”是什么命名空间。我发现:gemstone.com/docs/5.7.0/product/native_client/docs/… 上的 GemStone.GemFire.Cache!采取什么行动?我只能找到一个委托方法。谢谢。
      • @Rotaney:我不知道 log4net,这只是一个如何向函数添加参数的示例。另一种方法可能是拥有多个函数(信息、警告、错误等)并将所有这些函数传递给您的脚本。
      • 很酷的东西,但不幸的是,由于序列化,这不适用于 PSRemoting。 PSRemoting 还有办法做到这一点吗?
      【解决方案3】:

      我没有特定于 log4net 的详细信息,但应该可以像任何其他 .Net 组件一样加载 log4net:

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

      【讨论】:

        猜你喜欢
        • 2015-07-17
        • 2012-09-21
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多