我用 C# 编写了一个程序,它创建了一个日志文件并使用 log4net 填充它。该程序启动 powershell-scripts。脚本也应该使用 log4net。我想监视脚本在运行时记录到 log4net 的内容,并将此信息写入我的日志文件。
您知道我是如何做到这一点的,或者我从哪里可以获得有关我的问题的信息/帮助吗?
谢谢
最佳答案
您可以定义一些函数并将它们作为变量传递给您的脚本:
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);
关于c# - 从 powershell 脚本记录到 csharp 程序(log4net - 日志文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3977247/