0

We are using NLog in our solution, which is primarily a C# application. We do however have a native C++ DLL project and a managed C++ DLL project that wraps it. We would like to be able to log from those projects to NLog like we do throughout the rest of the application.

I found some hints online that this was possible, but all the trails went cold. https://stackoverflow.com/a/9213528/2212458

Does anyone know if this is possible or how to proceed? Thanks.

Community
  • 1
  • 1
denver
  • 2,863
  • 2
  • 31
  • 45

3 Answers3

2

This was supported in NLog 2 (called NLogC).

The support for it had been dropped in NLog 3: http://nlog-project.org/2013/12/14/nlog-3-0.html

Julian
  • 33,915
  • 22
  • 119
  • 174
  • Thanks. I see it now on the 2.1 tag. Wonder if it will work if I build it. – denver Jan 04 '17 at 20:55
  • 1
    It works! You can just take the NLogC project from the 2.1 tag, build it, and use it with the current NLog release (I used the one from Nuget). – denver Jan 04 '17 at 21:50
2

I would probably just add a C++/Interop-Helper-project to your solution, that implements an abstract C++ logging-interface, that calls into the .NET NLog-methods.

Then the Helper-project will work as interface between the C#-application and the existing C++ DLL-project. It will initialize the C++ DLL and give the abstract C++ logging-interface to the C++ DLL.

With this method, then you can use any version of NLog.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
1

I'm actually able to use NLog from a Managed C++ project and I haven't seen any other examples of this working. In fact, I don't think it's supported by the NLog development team. But it's nice to be able to add logging to a Managed C++ project. If you want instructions, I can provide it. If you're looking for a solution for an NLog solution for native C++, there was an older version 2.0 that supported it. See this example: How to use NLog in C++?

This is the code I'm running in a CLR console app VS 2013 with NLog 4.0.0:

using namespace NLog;

NLog::Logger^ LO = nullptr;
NLog::LogManager^ LM = nullptr;
LO = LM->GetCurrentClassLogger();
LO->Debug("Ha");
Charles Owen
  • 2,403
  • 1
  • 14
  • 25