我可以将输出从 C DLL 重定向到我的 c# log4net 输出吗?

Can I redirect output from a C DLL into my c# log4net output

本文关键字:输出 我的 log4net 重定向 DLL 我可以      更新时间:2023-10-16

我有一个C#应用程序,它反过来加载一个C或C++dll(反过来加载其他C/C++ dll)。 在 C# 应用程序中,我使用 log4net 记录器将所有输出捕获到一系列日志文件中。 我的应用程序作为 Windows 服务运行,因此没有控制台/输出窗口供正常的 printfs 或写入 stdout/stderr 的输出转到。

有没有办法将 C# 应用程序设置为定向 stdout/stderr(来自 DLL)并将每一行转换为 log4net 输出。 或者C/C++ DLL中是否有某种方法可以将stdout/stderr流连接到log4net输出?

我找到了一些解决方案(这里:http://bytes.com/topic/c-sharp/answers/822341-dllimport-stdout-gets-eaten),表明我需要像这样对我的 C DLL 进行调用: setvbuf(stdout, NULL, _IONBF, 0); 虽然,我不知道它做了什么,但它没有做我想要的。 我想我也需要一个类似的 stderr 行。 无论哪种情况,谷歌似乎都认为这些行只是负责缓冲,而不是重定向到log4net。

我假设我需要某种函数覆盖来阻止控制台写入(从另一种语言加载的 DLL)并将它们转换为mLog.InfoFormat("{0}", consoleString);类型的调用。 我是 c# 的新手,甚至不确定要谷歌什么术语才能找到这样的覆盖(如果可能的话)。

不确定这是否会使问题复杂化,但我的 C# 应用程序是多线程的,并且某些 DLL 也具有多个线程。 我认为这只是意味着我需要在处理控制台输出并将其写入 log4net 框架(也许)的方法中具有某种锁,或者 log4net 的正常序列化将为我处理它。

事实证明,

一旦我想出如何使用它们,这些就起作用了。 我设置了两个命名管道(或同一管道的两端? 一个我连接到stdout,并让它在log4net中对通过管道发送的任何内容的日志消息。

internal static void InfoLogWriter(Object threadContext)
{
    mLog.Info("InfoLogWriterthread started");
    int id = Process.GetCurrentProcess().Id; // make this instance unique
    var serverPipe = new NamedPipeServerStream("consoleRedirect" + id, PipeDirection.In, 1);
    NamedPipeClientStream clientPipe = new NamedPipeClientStream(".", "consoleRedirect" + id, PipeDirection.Out, PipeOptions.WriteThrough);
    mLog.Info("Connecting Client Pipe.");
    clientPipe.Connect();
    mLog.Info("Connected Client Pipe, redirecting stdout");
    HandleRef hr11 = new HandleRef(clientPipe, clientPipe.SafePipeHandle.DangerousGetHandle());
    SetStdHandle(-11, hr11.Handle); // redirect stdout to my pipe
    mLog.Info("Redirection of stdout complete.");
    mLog.Info("Waiting for console connection");
    serverPipe.WaitForConnection(); //blocking
    mLog.Info("Console connection made.");
    using (var stm = new StreamReader(serverPipe))
    {
        while (serverPipe.IsConnected)
        {
            try
            {
                string txt = stm.ReadLine();
                if (!string.IsNullOrEmpty(txt))
                    mLog.InfoFormat("DLL MESSAGE : {0}", txt);
            }
            catch (IOException)
            {
                break; // normal disconnect
            }
        }
    }
    mLog.Info("Console connection broken.   Thread Stopping.");
}

还有一个函数可以将所有这些推送到另一个线程,这样当它命中各种阻塞调用时,它就不会阻塞我的主线程。

internal static void RedirectConsole()
{
    mLog.Info("RedirectConsole called.");
    ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(InfoLogWriter));
    // TODO enqueue and item for error messages too.
}

我在断开连接时遇到问题,必须重新连接管道,但我会想出重新连接解决方案。 我猜当 DLL 从内存中交换回来时,或者当我需要读取但当前没有任何准备读取的内容时,就会发生这种情况? 我还必须设置另一对来阻止 stderr 并重定向它,使用该对的错误日志。 可能想摆脱幻数(-11)并使用普通枚举(STD_ERROR_HANDLE等)

相关文章: