Log to event viewer on Windows with C++
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.
![Leonardo Alves Machado](https://i.stack.imgur.com/VoFWS.jpg?s=256&g=1)
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, 2022Comments
-
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 about 8 yearsAs 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 about 8 yearsThanks, I didn't know that - I really found that weird
-
Jesper Juhl about 8 yearsTry reading the documentation.
-
Donnie about 8 yearsReally, this isn't off topic. It's BROAD, but it's asking for help with a specific component of windows.
-
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 about 8 yearsIt'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 about 8 yearsWhat 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 over 5 yearsI'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 over 3 yearsI 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 about 7 yearsIn my case I needed to write
LPCWSTR custom_log_name = L"MyLogName";
andLPCWSTR message = L"I'm in an event log";
instead ofconst char*
-
Paulus over 3 yearsThanks for this, it does work, but writes to Windows Logs/Application. Any idea how to write to 'Application and Services Logs/MyCompany/MyApplication/Operation' ?