How to use NLog in C++?

2019-04-07 12:23发布

I have a simple native ++ console application in visual C++.

In order to use NLog there is a mixed mode " "NLogC.dll"

  • How can i add "NLogC.dll" to my application
  • And use for logging?

Simply how can i use Nlog in a native C++ Application?

1条回答
走好不送
2楼-- · 2019-04-07 13:10

NLog includes a header file (NLogC.h) and import library (NLogC.lib). Those should be used to use the library.

Add the path to the include file (e.g. C:\Program Files (x86)\NLog\.NET Framework 4.0\NLogC\include) to the include path, either globally or for the project only. You can specify it in the project's properties under "Additional Include Directories" under Configuration Properties, C/C++, General. Add the path to the library file (e.g. C:\Program Files (x86)\NLog\.NET Framework 4.0\NLogC\x86; make sure to pick x86 or x64 based on the architecture you're targeting) to the library path ("Additional Library Directories" under Configuration Properties, Linker, General).

Add the NLogC.lib file to the project's libraries (add it to "Additional Dependencies" under Configuration Properties, Linker, Input).

Then, you can use the API like this:

#include <cstdarg> // Needed for va_list type, which NLogC.h requires
#include <NLogC.h>

int main()
{
    NLog_Info(L"Test", L"TestMessage");

    return 0;
}

Make sure you put NLogC.dll, NLog.dll, and a suitable configuration file in the same directory as your executable.

Note that this is really only intended to be used when you have native components as part of a larger, managed application, or are transitioning from native to managed. If your application is pure C++, there are likely more suitable native logging libraries that don't require loading the CLR just to do logging.

查看更多
登录 后发表回答