Header Ads

WPF: How to Create Windows Application Event Logs

When developing a desktop-based software or application, keeping track of internal process very difficult unlike web development. For that matter i.e. in order to either understanding the cause of error generated by the desktop software or make internal processes confirmation, the idea is to generate log file or register log entries for a particular event happens internally within the targeted software. You can either generate a log file with your software or you can register target log message into windows application log event.

Today, I shall be demonstrating registration of your targeted software's event log entry into windows application event logs using WPF application.
 


Prerequisites:

Following are some prerequisites before you proceed any further in this tutorial:
  1. Knowledge of WPF Application Development.
  2. Knowledge of C# Programming.
The example code is being developed in Microsoft Visual Studio 2019 Professional. 

Download Now!

Let's begin now.

1) Create new C#.NET WPF application and name it "WPFWindowsEventLog".  
 
2) Create "Views/HomePage.xaml" file.
 
3) Open "Views/HomePage.xaml.cs" file and write following lines of code within a method 'LogEventMessage(...)', whenever you want to register a log message with windows application event logs i.e.

...

private void LogEventMessage(string msg)
{

...

       // Create an EventLog instance and assign its source.
       EventLog myLog = new EventLog();
       myLog.Source = this.logSrcName;

       // Write an informational entry to the event log.    
       myLog.WriteEntry(msg, EventLogEntryType.Information, this.winEventID);

...

}

...

In the above lines of code, know that 'logsrcName' and 'winEventID' variables will uniquely identify your log entry inside windows application log event You can then utilize the 'LogEventMessage(...)' method whenever you want your software to output important internal messages related to errors or plain information.

4) Whatever log source name you choose, you need to register that name into windows system registry, by executing below registry line i.e.

...

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application\MyAppLogSrc]

...

5) Now, execute the project and start registering the windows log event, you will be able to see the following i.e.


Now, open 'Computer Management' by opening windows 'Run' command and type 'CompMgmtLauncher' command. Then under Computer Management (Local) -> System Tools -> Event Viewer -> Application and you will see following i.e.




Conclusion

In this article, you will learn registration of your targeted software's event log entry into windows application event logs using WPF application. You will also learn to create windows registry entry for your windows application log event identification and you will also learn about looking into your windows application log events.

No comments