Simple test with VS2008 C++/CLI

2 messages Options
Embed this post
Permalink
Tony Duarte

Simple test with VS2008 C++/CLI

Reply Threaded More More options
Print post
Permalink
From C++/CLI I got a log to both console and file by:
1) Modified Project->Properties->Configuration Properties->General->
        Common Language Runtime support-> /CLR
2) Copied NLog.dll from installation dir to VS Debug directory so #using can find.
3) Created NLog.config in VS Debug directory for this project:
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
        <target name="console" xsi:type="Console" layout="${date:format=HH\:MM\:ss} ${logger} ${message}" />
        <target name="file" xsi:type="File" fileName="${basedir}/DBGfile.txt"
            layout="${stacktrace} ${message}"/>
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="console,file" />
    </rules>
</nlog>

I used the following test code:
#include "stdafx.h"
#using "NLog.dll"    // reference the assembly. Requires C++ compilation with /clr
using namespace NLog;

ref class Program  {
public:
        Program(){};
        ~Program(){};
        void doIt(){
                Logger^ logger;
                logger = LogManager::GetCurrentClassLogger();
                logger->Debug("Hello World!");
        }
 };

int _tmain(int argc, _TCHAR* argv[])
{
        Program^ myProg = gcnew Program();
        myProg->doIt();
        return 0;
}
Tony Duarte

Re: Simple test with VS2008 C++/CLI

Reply Threaded More More options
Print post
Permalink
I was integrating into my C++/CLI project and I started getting:
  "An unhandled exception of type 'System.NullReferenceException' occurred in NLog.dll
  Additional information: Object reference not set to an instance of an object."

In hindsight, the reason is obvious. I had put the logging commands into a class that wasn't
declared as a "ref class".  A trivial sample that generates this failure follows:

class Program  {
public:
        Program(){};
        ~Program(){};
        void doIt(){
                Logger^ logger = LogManager::GetCurrentClassLogger();
        logger->Debug("Hello World!");
        }
 };

int _tmain(int argc, _TCHAR* argv[])
{
        Program myProg;
        myProg.doIt();
        return 0;
}