How do I log events in Windows IoT?

2019-01-20 04:54发布

问题:

WebUI for Windows IoT (as seen on Raspberry PI) features the following Event Tracing screen:

How do I write into an event log from a uwp program and then later examine what's written there?

This answer (which is not about Windows IoT) suggests that you need to use standard System.Diagnostics.Tracing classes for it. The sample that this answer is referring to is to demonstrate logging to a file. I'm more after logging to a built-in facility, such as ETW.

Following similar logic as in the sample I'm executing EventSource.Write and it does not throw a error, but written message is nowhere to be seen.

Windows Powershell for Window IoT does not support Get-EventLog command either.

Windows IoT development is relatively new, so there is not much information in the internet. The best source currently is Ms Iot Samples but despite huge number of samples none of them seem to be doing any event logging.

回答1:

  1. Read and understand EventSource User’s Guide
  2. Add a class to your project that derives from EventSource and implement your methods as described in the linked guide. A sample implementation is available here.
  3. Build your project, locate project output (yourProject.winmd)
  4. Get EventRegister.exe utility from here
  5. Run EventRegister.exe -UsersGuide. Read and understand usage.
  6. Run eventRegister.exe -DumpRegDlls yourProject.winmd. This will generate yourProject.MyEwtProvider.etwManifest.dll and yourProject.MyEwtProvider.etwManifest.man files.
  7. Create a new folder on your Windows IoT device, for example, C:\Events or use an existing one.
  8. Open yourProject.MyEwtProvider.etwManifest.man in an editor and edit resourceFileName and messageFileName paths so that the parent folder is C:\Events
  9. Copy yourProject.MyEwtProvider.etwManifest.dll and yourProject.MyEwtProvider.etwManifest.man over to your Windows IoT to the C:\Events folder. Note: the simplest way to do this is to open windows share as \\yourdevice\c$ in Windows Explorer
  10. Connect to your Windows IoT with Powershell. Run cd C:\Events
  11. Run wevtutil.exe im .\yourProject.MyEwtProvider.etwManifest.man. This should not produce any warnings or errors. Please refer to this page for the details on wevtutil.exe syntax.

Now if you navigate to your ETW WebUI page as in your question "MyEwtProvider" will appear in the drop down. You log events in your program by calling one of the UwpEventSource.Log.Info/Warn/Debug/Critical/Error("Hello from my porgram");

If you do not want to collect the events when you are not working with the WebUI you are done. If you want to be able to persist these and analyse them later, run the following command in your powershell session:

echo y | wevtutil.exe sl MyEwtProvider/Debug /e:true

See what it does here. Now you will be able to retrieve historical data (once you've accumulated them) by running:

wevtutil.exe qe MyEwtProvider/Debug