Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NLog in C++?

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?

like image 221
Novalis Avatar asked Jan 31 '12 09:01

Novalis


People also ask

How do you write an NLog?

Configuration in C# using NLog; using NLog. Config; using NLog. Targets; ... var config = new LoggingConfiguration(); var consoleTarget = new ConsoleTarget { Name = "console", Layout = "${longdate}|${level:uppercase=true}|${logger}|${message}", }; config.

What are the NLog levels?

Writing Log Messages With NLog The available methods for logging are (in ascending order): Trace, Debug, Info, Warn, Error, and Fatal. Each of these methods creates a log message with a corresponding log level—an indicator of the message's importance.


1 Answers

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.

like image 129
Sven Avatar answered Oct 17 '22 17:10

Sven