在MFC应用程序中使用Boost事件记录器:找不到事件ID的描述

时间:2015-09-02 12:18:57

标签: c++ logging mfc event-log boost-log

我尝试使用Boost日志库向Windows事件日志发送事件。我为自定义记录器制作了简单的C ++测试应用程序,一切都很好。 当我在MFC应用程序中使用记录器时,在EventViewer中可见的生成事件具有此错误

  

来自源MFCApplication1的事件ID 259的描述不能   被发现....

当我在Boost documentation中读到 basic_simple_event_log_backend "包含事件查看器正常运行所需的所有资源,并在Windows注册表中注册Boost.Log库为了将自己填充为这些资源的容器。"但我想有些事情失败了。

知道如何在MFC应用程序中使用它吗?我使用的是Boost 1.59 32bit和VS2013。

对于复制,我将MFC应用程序简化为单个cpp文件,没有任何资源:

#define VC_EXTRALEAN
#include <SDKDDKVer.h>
#define _AFX_ALL_WARNINGS
#include <afxwin.h>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/expressions/keyword.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/core.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sink.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/event_log_backend.hpp>
#include <iomanip>

namespace logging
{
    enum severity_level
    {
        fatal,
        error,
        warning,
        info,
        debug,
        trace,
    };

    typedef boost::log::sources::severity_logger_mt<severity_level> dispatcher_type;
    dispatcher_type disp;

    BOOST_LOG_ATTRIBUTE_KEYWORD( severity, "Severity", severity_level)

    void init( const std::string& appName)
    {
        namespace lg        = boost::log;
        namespace src       = boost::log::sources;
        namespace sinks     = boost::log::sinks;
        namespace keyword   = boost::log::keywords;
        namespace expr      = boost::log::expressions;
        namespace attrs     = boost::log::attributes;

        typedef sinks::synchronous_sink<sinks::simple_event_log_backend> evsink;
        auto sink = boost::make_shared<evsink>( keyword::log_source = appName);
        sink->set_formatter( expr::stream << expr::smessage);

        sinks::event_log::custom_event_type_mapping<severity_level> mapping( severity.get_name());
        mapping[ fatal]     = sinks::event_log::error;
        mapping[ error]     = sinks::event_log::error;
        mapping[ warning]   = sinks::event_log::warning;
        mapping[ info]      = sinks::event_log::info;
        mapping[ debug]     = sinks::event_log::info;
        mapping[ trace]     = sinks::event_log::info;

        sink->locked_backend()->set_event_type_mapper( mapping);
        sink->set_filter( severity < info);
        lg::core::get()->add_sink( sink);
    }
}

#define LOG_ERROR BOOST_LOG_FUNCTION();BOOST_LOG_SEV(logging::disp, logging::error)

class CMFCApplication1App : public CWinApp
{
public:
    const std::string appName = "MFCApplication1";

    CMFCApplication1App()
    {
        if ( 0)
        {
            logging::init( appName);

            LOG_ERROR << "hello1"; // // leads to a failed assert in
            //>MFCApplication1.exe!boost::intrusive_ptr<boost::log::v2s_mt_nt5::attributes::named_scope::impl>::operator->() Line 174   C++
            // MFCApplication1.exe!boost::log::v2s_mt_nt5::attributes::named_scope::push_scope(const boost::log::v2s_mt_nt5::attributes::named_scope_entry & entry) Line 289    C++
            // MFCApplication1.exe!boost::log::v2s_mt_nt5::attributes::named_scope::sentry::sentry(const boost::log::v2s_mt_nt5::basic_string_literal<char,std::char_traits<char> > & sn, const boost::log::v2s_mt_nt5::basic_string_literal<char,std::char_traits<char> > & fn, unsigned int ln, boost::log::v2s_mt_nt5::attributes::named_scope_entry::scope_name_type t) Line 376    C++
            // MFCApplication1.exe!CMFCApplication1App::CMFCApplication1App() Line 71   C++
            // MFCApplication1.exe!`dynamic initializer for 'theApp''() Line 92 C++
        }
    }

    virtual BOOL InitInstance()
    {
        if ( 1)
        {
            logging::init( appName);

            LOG_ERROR << "hello2"; // leads to unknown event id
            // The description for Event ID 259 from source MFCApplication1 cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
            // If the event originated on another computer, the display information had to be saved with the event.
            // The following information was included with the event: 
            // hello2
            // The specified resource type cannot be found in the image file
        }

        return TRUE;
    }

    virtual int ExitInstance()
    {
        return CWinApp::ExitInstance();
    }
};

CMFCApplication1App theApp;

0 个答案:

没有答案