首页 > 解决方案 > .NET 分析 API 初始化

问题描述

我正在尝试学习如何使用分析 API 编写一个简单的 .net 分析器。

第一步,我只想能够加载分析器 dll 并从 ICorProfilerCallback::Initialize 写入日志文件。

在一个测试 .net 控制台应用程序中,我设置了以下环境变量:

COR_ENABLE_PROFILING="1"

COR_PROFILER="ProfilerTest"--> 这就是我认为的问题所在。我不知道如何找到 GUID,而 ProfilerTest 是我的 dll 的名称。这里说从 .NET Framework 4 开始,不必注册分析器。这是否意味着不需要设置这个环境变量?

在微软的 CLRProfiler 源代码中,他们还设置了COR_PROFILER_PATH.

这里要完整的是来自 dll 的初始化函数:

HRESULT STDMETHODCALLTYPE Profiler::Initialize(IUnknown* pICorProfilerInfoUnk)
{
    // A macro that writes to a log file.
    LOG(INFO);
    auto queryInterfaceResult = pICorProfilerInfoUnk->QueryInterface(__uuidof(ICorProfilerInfo), reinterpret_cast<void **>(&this->corProfilerInfo));

    if (FAILED(queryInterfaceResult))
        return E_FAIL;

    DWORD eventMask = COR_PRF_ALL;

    auto hr = this->corProfilerInfo->SetEventMask(eventMask);

    if (hr != S_OK)
        printf("ERROR: Profiler SetEventMask failed (HRESULT: %d)", hr);

    printf("ERROR: Profiler SetEventMask failed (HRESULT: %d)", hr);
    LOG(INFO);

    return S_OK;
}

标签: .netcomprofilingclrclr-profiling-api

解决方案


推荐阅读