Log to event viewer on Windows with C++

11,748

Your link doesn't compile because that's managed C++ (note the use of gcnew)

If all you want to write are strings it's easy, all you need is RegisterEventSource and ReportEvent.

It's approximately this:

const char* custom_log_name = "MyLogName";

// create registry keys for ACLing described on MSDN: http://msdn2.microsoft.com/en-us/library/aa363648.aspx

HANDLE event_log = RegisterEventSource(NULL, custom_log_name);
const char* message = "I'm in an event log";
ReportEvent(event_log, EVENTLOG_SUCCESS, 0, 0, NULL, 1, 0, &message, NULL);

This only allows for logging strings. Much more complex (and useful) logging is possible, but it's fairly involved in straight C++. If you can write managed code for your logging component it becomes easier to deal with.

Share:
11,748
Leonardo Alves Machado
Author by

Leonardo Alves Machado

Developer and Analyst C/C++ .Net/C# Java Javascript OpenGL OpenCV - average on everything, growing with every opportunity... Check the top 1000 users from Brazil at stackoverfow. There are some helpful people in my country...

Updated on June 04, 2022

Comments

  • Leonardo Alves Machado
    Leonardo Alves Machado about 2 years

    I want to use log on my C++ application. However, I'd like to use Windows (10) event viewer, instead of text files. I found out some weird calls, that I don't even know what the parameters mean - ReportEvent, OpenEventLog and some other Event Logging functions. I also can't use managed code, due to some limitations on my app.

    I've also tried to use the code on this link, but I get compilation errors (namespace 'System' undefined - it seems some include files are missing...).

    I found no sample code that works yet.

    I would appreciate a sample code, if possible - just a simple logging from a local application, built in non managed C++. Can someone help?

    • user4581301
      user4581301 about 8 years
      As soon as you see the 'System' namespace, you've wandered in to managed-land and can stop looking for unmanaged code examples.
    • Leonardo Alves Machado
      Leonardo Alves Machado about 8 years
      Thanks, I didn't know that - I really found that weird
    • Jesper Juhl
      Jesper Juhl about 8 years
      Try reading the documentation.
    • Donnie
      Donnie about 8 years
      Really, this isn't off topic. It's BROAD, but it's asking for help with a specific component of windows.
    • Jesper Juhl
      Jesper Juhl about 8 years
      @Donnie it's still a rather bad question though. No minimal testable code. No indication of what the asker has already tried, etc.. Smells like someone who just wants to be spoon-fed a solution.
    • Donnie
      Donnie about 8 years
      It's true, but to be fair, the event logging document on msdn for C++ is pretty darn obtuse. I was pretty intimidated by it the first few times through too.
    • Leonardo Alves Machado
      Leonardo Alves Machado about 8 years
      What you mean? I posted the functions I was looking for, I said I didn't understand the parameters. Posted the pages I've read. My question is pretty clear and direct, about a subject that is not very well documented on the API - lots of parameters, that could be swapped by NULL or zero, but I didn't know...
    • jmgonet
      jmgonet over 5 years
      I've tried to log events in windows myself, and I can say it is perfectly understandable why there is no minimal testable code: after several days, I'm clueless. This question is spot on, and got dismissed; I'm frustrated.
    • Paulus
      Paulus over 3 years
      I agree, this question is very pertinent, I am looking for an answer myself, and Microsoft's documentation on this is abysmal, with no overview or coherent examples
  • Alexander Tumanin
    Alexander Tumanin about 7 years
    In my case I needed to write LPCWSTR custom_log_name = L"MyLogName"; and LPCWSTR message = L"I'm in an event log"; instead of const char*
  • Paulus
    Paulus over 3 years
    Thanks for this, it does work, but writes to Windows Logs/Application. Any idea how to write to 'Application and Services Logs/MyCompany/MyApplication/Operation' ?