Log to event viewer on Windows with C++ [closed]
Asked Answered
C

1

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?

Congou answered 4/5, 2016 at 19:4 Comment(9)
As soon as you see the 'System' namespace, you've wandered in to managed-land and can stop looking for unmanaged code examples.Sinkage
Thanks, I didn't know that - I really found that weirdCongou
Try reading the documentation.Dhobi
Really, this isn't off topic. It's BROAD, but it's asking for help with a specific component of windows.Sorus
@Sorus 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.Dhobi
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.Sorus
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...Congou
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.Annunciator
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 examplesCheerly
S
18

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.

Sorus answered 4/5, 2016 at 19:15 Comment(4)
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*Oneirocritic
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' ?Cheerly
Thanks. I knew there had to be a way other than defining and compiling a manifest and all that jazz. Now just need to figure out where the default category and event ID constants are. They have to be somewhere...Woodworth
Well, the problem is, at least on this machine (win11), the string is logged but only if you drill into the details of the entry. The primary "General" tab displays "The operation completed successfully." because it's using FormatMessage() to obtain the string from event ID (0 = no error).Woodworth

© 2022 - 2024 — McMap. All rights reserved.