Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

c# - How do I log events in Windows IoT?

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

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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 \yourdevicec$ 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...