10

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?

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
  • 2
    As soon as you see the 'System' namespace, you've wandered in to managed-land and can stop looking for unmanaged code examples. – user4581301 May 04 '16 at 19:15
  • Thanks, I didn't know that - I really found that weird – Leonardo Alves Machado May 04 '16 at 19:18
  • Try reading the documentation. – Jesper Juhl May 04 '16 at 19:54
  • 2
    Really, this isn't off topic. It's BROAD, but it's asking for help with a specific component of windows. – Donnie May 04 '16 at 20:00
  • @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. – Jesper Juhl May 04 '16 at 20:13
  • 3
    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. – Donnie May 04 '16 at 20:36
  • 2
    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... – Leonardo Alves Machado May 04 '16 at 21:05
  • 3
    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. – jmgonet Jan 03 '19 at 18:03
  • 1
    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 – Paulus Oct 15 '20 at 14:59

1 Answers1

16

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.

Donnie
  • 45,732
  • 10
  • 64
  • 86
  • 1
    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*` – Alexander Tumanin May 18 '17 at 13:21
  • 2
    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' ? – Paulus Oct 15 '20 at 15:01