Log4net,将日志消息从 c++ dll 发送到 c# 应用程序?
Log4net, sending log messages from a c++ dll to a c# application?
我有一个使用 Log4Net 进行日志记录的 WPF/c# 应用程序。此应用程序使用以下方法调用几个 c++ dll:
[DllImport("test.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void TestFunction();
我想做的是让 dll 将日志记录消息发送回 C# 应用程序,以便 c++ 和 c# 中的所有内容都转到同一日志。这可能吗?
如果是这样,我该怎么做?
将c++ dill中的日志重定向到 c# 回调的示例:
C# 端:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void LogCallback([MarshalAs(UnmanagedType.LPWStr)] string info);
namespace WinApp
{
static class Wrapper
{
[DllImport("target.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetLogCallback([MarshalAs(UnmanagedType.FunctionPtr)] LogCallback callbackPointer);
internal static void Init()
{
LogCallback log_cb = (info) =>
{
Console.Write(DateTime.Now.TimeOfDay.ToString() + " " + info);
};
SetLogCallback(log_cb);
}
}
C++端(编译target.dll
(:
extern "C" {
typedef char* (__stdcall* LogCallback)(const wchar_t* info);
PEPARSER_API void SetLogCallback(LogCallback cb);
}
static LogCallback global_log_cb = nullptr;
void LogInfo(const wchar_t* info) {
if (global_log_cb) {
std::wstring buf = L"[PEParse]" + std::wstring(info) + L"n";
global_log_cb(buf.c_str());
}
else {
std::cout << "global_log_cb not setn";
}
}
void LogInfo(const char* info) {
const size_t cSize = strlen(info) + 1;
size_t t;
std::wstring wstr(cSize, L'#');
mbstowcs_s(&t, &wstr[0], cSize, info, cSize - 1);
LogInfo(&wstr[0]);
}
void SetLogCallback(LogCallback cb) {
global_log_cb = cb;
LogInfo("log init");
}
我已经使用这个界面很长时间了。
相关文章:
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- C# DLL 从C++应用程序调用 SOAP Web 服务
- 将 c++ 生成器应用程序与外部 dll 结合使用
- WinDeployqt 不会为调试应用程序部署 Qwindowsd.dll
- Log4net,将日志消息从 c++ dll 发送到 c# 应用程序?
- 调试 ASP MVC 应用程序中使用的C++ DLL
- Qt应用程序找不到第三方DLL并崩溃
- 如何将从控制台应用程序C++回调设置为C++ COM DLL
- 我可以用C++/WinRT编写一个DLL,供windows桌面应用程序(而不是UWP)或Unity插件使用吗
- .NET核心应用程序无法加载C++DLL
- 在通用 Windows 应用程序C++上使用 win32 DLL 库
- 从 C# Windows 应用程序调用 C dll 会导致 svchost.exe 崩溃
- 使用 C++ dll 运行 c# 应用程序会生成无法加载异常
- 为什么我的 C# 应用程序无法加载我的 C++ dll?
- 应用程序仅在qt调试器中加载dll,而不是独立加载
- 如果在 DLL 和调用应用程序中使用 GPGPU API,会发生什么情况
- C++ XE 5 应用程序和C++生成器 5 dll 兼容性
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- 使用Visual Studio 2015创建的C 应用程序分发MSVCR120.dll