Skip to content

Unable to see messages properly in event viewer #169

@humayunkhan

Description

@humayunkhan

Hi,

Following are my environment details:
I am using Boost Library version 1.77.0
I am using Windows 10 operating system.
I am using Visual Studio 2019.
The platform toolset is v142
C++ language standard: ISO C++ 17 (/std:c++17)
C Language standard: ISO C 17 (2018) (/std:c17)
My project is a DLL project where I am using event logging
I am also using the following macro: #define BOOST_ALL_DYN_LINK

I am using custom event logging as described in the following link:
https://www.boost.org/doc/libs/1_77_0/libs/log/example/event_log/main.cpp

I have created a custom message configuration file with the following content:
`
; /* --------------------------------------------------------
; HEADER SECTION
; */
SeverityNames=(Debug=0x0:AUTO_SCORE_SEVERITY_DEBUG
Info=0x0:AUTO_SCORE_SEVERITY_INFO
Warning=0x2:AUTO_SCORE_SEVERITY_WARNING
Error=0x1:AUTO_SCORE_SEVERITY_ERROR
)

; /* --------------------------------------------------------
; MESSAGE DEFINITION SECTION
; */

MessageIdTypedef=WORD

MessageId=0x1
SymbolicName=CATEGORY_DEBUG
Language=English
Debug Message
.

MessageId=0x2
SymbolicName=CATEGORY_INFO
Language=English
Info Message
.

MessageId=0x3
SymbolicName=CATEGORY_WARNING
Language=English
Warning Message
.

MessageId=0x4
SymbolicName=CATEGORY_ERROR
Language=English
Error Message
.

MessageIdTypedef=DWORD

MessageId=0x100
Severity=Debug
Facility=Application
SymbolicName=DEBUG_MSG
Language=English
[%1][%2]: %3
.

MessageId=0x101
Severity=Info
Facility=Application
SymbolicName=INFO_MSG
Language=English
.

MessageId=0x102
Severity=Warning
Facility=Application
SymbolicName=WARNING_MSG
Language=English
[%1][%2]: %3
.

MessageId=0x103
Severity=Error
Facility=Application
SymbolicName=ERROR_MSG
Language=English
[%1][%2]: %3
.

`

The logging header file looks like this:
`
#pragma once
#include
#include
#include
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

#include <boost/log/common.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/event_log_backend.hpp>
#include "autoscore_eventlog_provider/autoscore_eventlog_provider.h"

// Check windows
#if _WIN32 || _WIN64
#if _WIN64
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif

// Check GCC
#if GNUC
#if x86_64 || ppc64
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif

#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>

namespace autoscore::log
{
namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;

// Define application-specific severity levels
enum severity_level
{
	debug,
	info,
	warning,
	error
};

//Define AutoScore Logging Config
struct config
{
	severity_level min_logging_level;
	std::string message_file;
	std::string log_name;
	std::string log_source;
	bool enable_win_event_log;
};

void init_logging(config logging_config);

//[ example_sinks_event_log_facilities
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(event_logger, src::severity_logger_mt< severity_level >)


/// <summary>
/// The function raises an event with a debug message
/// </summary>
/// <param name="message"></param>
/// <param name="session_id"></param>
void log_debug(std::string const& message, std::string const& session_id = "N/A");

/// <summary>
/// The function raises an event with a info message
/// </summary>
/// <param name="message"></param>
/// <param name="session_id"></param>
void log_info(std::string const& message, std::string const& session_id = "N/A");


/// <summary>
/// The function raises an event with a warning message
/// </summary>
/// <param name="message"></param>
/// <param name="session_id"></param>
void log_warning(std::string const& message, std::string const& session_id = "N/A");


/// <summary>
/// The function raises an event with a error message
/// </summary>
/// <param name="message"></param>
/// <param name="session_id"></param>
void log_error(std::string const& message, std::string const& session_id = "N/A");

}
`

The logging implementation file looks like this:
`
#include "pch.h"
#include "autoscore_log.h"

namespace autoscore::log
{
void init_win_event_log(config const logging_config)
{
// Create an event log sink
boost::shared_ptr< sinks::event_log_backend > backend(
new sinks::event_log_backend((
keywords::message_file = logging_config.message_file, //%APPDATA%\Holberg EEG AS\autoSCORE\Logs\autoscore_eventlog_provider.dll
keywords::log_name = logging_config.log_name, //Holberg EEG AS
keywords::log_source = logging_config.log_source //autoSCORE
))
);

    // Create an event composer. It is initialized with the event identifier mapping.
    sinks::event_log::event_composer composer(
        sinks::event_log::direct_event_id_mapping< int >("EventID"));

    // For each event described in the message file, set up the insertion string formatters
    composer[DEBUG_MSG]
        % expr::attr< std::string >("TimeStamp")
        % expr::attr< std::string >("SessionID")
        % expr::attr< std::string >("Message");

    composer[WARNING_MSG]
        % expr::attr< std::string >("TimeStamp")
        % expr::attr< std::string >("SessionID");
    composer[ERROR_MSG]
        % expr::attr< std::string >("TimeStamp")
        % expr::attr< std::string >("SessionID");

    // Then put the composer to the backend
    backend->set_event_composer(composer);

    auto severity = boost::log::expressions::attr<severity_level>("Severity");

    // We'll have to map our custom levels to the event log event types
    sinks::event_log::custom_event_type_mapping< severity_level > type_mapping("Severity");
    type_mapping[debug] = sinks::event_log::make_event_type(AUTO_SCORE_SEVERITY_DEBUG);
    type_mapping[info] = sinks::event_log::make_event_type(AUTO_SCORE_SEVERITY_INFO);
    type_mapping[warning] = sinks::event_log::make_event_type(AUTO_SCORE_SEVERITY_WARNING);
    type_mapping[error] = sinks::event_log::make_event_type(AUTO_SCORE_SEVERITY_ERROR);

    backend->set_event_type_mapper(type_mapping);

    // Usually event categories can be restored by the event identifier.
    sinks::event_log::custom_event_category_mapping< int > cat_mapping("EventID");
    cat_mapping[DEBUG_MSG] = sinks::event_log::make_event_category(CATEGORY_DEBUG);
    cat_mapping[INFO_MSG] = sinks::event_log::make_event_category(CATEGORY_INFO);
    cat_mapping[WARNING_MSG] = sinks::event_log::make_event_category(CATEGORY_WARNING);
    cat_mapping[ERROR_MSG] = sinks::event_log::make_event_category(CATEGORY_ERROR);

    backend->set_event_category_mapper(cat_mapping);

    // Create the frontend for the sink
    boost::shared_ptr< sinks::synchronous_sink< sinks::event_log_backend > > sink(
        new sinks::synchronous_sink< sinks::event_log_backend >(backend));

    // Set up filter to pass only records that have the necessary attribute
    sink->set_filter(expr::has_attr< int >("EventID"));
    sink->set_filter(severity >= logging_config.min_logging_level);

    logging::core::get()->add_sink(sink);
}

void init_logging(config const logging_config)
{
    if (logging_config.enable_win_event_log)
    {
        init_win_event_log(logging_config);
    }
}

void log_debug(std::string const& message, std::string const& session_id)
{
    auto time_stamp = boost::posix_time::to_simple_string(boost::posix_time::microsec_clock::local_time());
    BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)DEBUG_MSG);
    BOOST_LOG_SCOPED_THREAD_TAG("SessionID", session_id);
    BOOST_LOG_SCOPED_THREAD_TAG("TimeStamp", time_stamp);
    BOOST_LOG_SCOPED_THREAD_TAG("Message", message);
    BOOST_LOG_SEV(event_logger::get(), debug) << "[" << time_stamp << "][" << session_id << "]: " << message;
}

void log_info(std::string const& message, std::string const& session_id)
{
    auto time_stamp = boost::posix_time::to_simple_string(boost::posix_time::microsec_clock::local_time());
    BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)INFO_MSG);
    BOOST_LOG_SCOPED_THREAD_TAG("SessionID", session_id);
    BOOST_LOG_SCOPED_THREAD_TAG("TimeStamp", time_stamp);
    BOOST_LOG_SCOPED_THREAD_TAG("Message", message);
    BOOST_LOG_SEV(event_logger::get(), info) << message;
}

void log_warning(std::string const& message, std::string const& session_id)
{
    auto time_stamp = boost::posix_time::to_simple_string(boost::posix_time::microsec_clock::local_time());
    BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)WARNING_MSG);
    BOOST_LOG_SCOPED_THREAD_TAG("SessionID", session_id);
    BOOST_LOG_SCOPED_THREAD_TAG("TimeStamp", time_stamp);
    BOOST_LOG_SEV(event_logger::get(), warning) << message;
}

void log_error(std::string const& message, std::string const& session_id)
{
    auto time_stamp = boost::posix_time::to_simple_string(boost::posix_time::microsec_clock::local_time());
    BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)ERROR_MSG);
    BOOST_LOG_SCOPED_THREAD_TAG("SessionID", session_id);
    BOOST_LOG_SCOPED_THREAD_TAG("TimeStamp", time_stamp);
    BOOST_LOG_SEV(event_logger::get(), error) << message;
}

}

`

I have created the header file and definitions DLL based on the *.mc file and created the registry keys by running the DLL consumer application as administrator.

I am able to see the events in the event viewer but without any content. I am attaching screen shots of how it looks like.

I am not sure what is my mistake. Can some one help m
event_log_error
e?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions