[Part 6] Logging with Universal Apps

Check out the codes in GitHub.

Basically, 2 steps:

Create a C++ Windows Runtime Component (dll) that will use the ETW header file. Reference the WinRT DLL to C# project.

WinRT DLL

My logging class looks like this:

public ref class TraceCore sealed
{
private:
    TraceCore();
    ~TraceCore();
          
    static TraceCore^ m_Instance;

public:
    static property TraceCore^ Instance
    {
        TraceCore^ get()
        {
            if (m_Instance == nullptr)
            {
                m_Instance = ref new TraceCore();
            }
            
            return m_Instance;
        }
    }

    void Verbose(Platform::String^ mod, Platform::String^ file, Platform::String^ func, Platform::String^ m);
};

And heres the implementation:

#include "pch.h"
#include "TraceCore.h"

using namespace LibRTWrapperETW;
using namespace Platform;

#include "jytrace.h"

TraceCore^ TraceCore::m_Instance = nullptr;

TraceCore::TraceCore()
{
    EventRegisterJyTrace();
}

TraceCore::~TraceCore()
{
    EventUnregisterJyTrace();
}

void TraceCore::Verbose(String^ mod, String^ file, String^ func, String^ m)
{
    EventWriteSimple(mod->Data(), file->Data(), func->Data(), L"Trace", m->Data());
}

Wrapper Class

Just like in part 5, I wrapped the C++ bits to a C# class so I can use the CallerMemberName and CallerFilePath attributes:

public static class TraceCoreWrapper
{
    public static void VerboseCore(
        string m,
        [CallerMemberName] string memberName = "?",
        [CallerFilePath] string srcFile = "?",
        [CallerLineNumber] int srcNum = 0)
    {
        TraceCore.Instance.Verbose("CORE_RT [" + Environment.CurrentManagedThreadId + "]", Path.GetFileName(srcFile), memberName, m);
    }
}

And finally, the actual logging in C#:

TraceCoreWrapper.VerboseCore("Hello from UApp (CS) world!");
---
If you have any questions or feedback, please reach out @flowerinthenyt.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.