- 1 year ago
Query for Event Log Information of Microsoft-Windows-UniversalTelemetryClient/Operational
To query the Event Log information for the Microsoft-Windows-UniversalTelemetryClient/Operational log in C#, you can make use of the EventLogQuery
and EventLogReader
classes from the System.Diagnostics.Eventing.Reader
namespace. Here's an example:
csharpusing System;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
class Program
{
static void Main()
{
string logName = "Microsoft-Windows-UniversalTelemetryClient/Operational";
// Define the query to retrieve all events from the log
string queryString = "*";
// Create an EventLogQuery object with the log name and query
EventLogQuery query = new EventLogQuery(logName, PathType.LogName, queryString);
try
{
// Create an EventLogReader object to read events from the query
using (EventLogReader reader = new EventLogReader(query))
{
EventRecord eventRecord;
// Read events until the end of the log
while ((eventRecord = reader.ReadEvent()) != null)
{
// Process each event as desired
Console.WriteLine("Event ID: " + eventRecord.Id);
Console.WriteLine("Event Time: " + eventRecord.TimeCreated);
Console.WriteLine("Event Level: " + eventRecord.LevelDisplayName);
Console.WriteLine("Event Message: " + eventRecord.FormatDescription());
Console.WriteLine();
}
}
}
catch (EventLogNotFoundException ex)
{
Console.WriteLine("Event log not found: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Error reading event log: " + ex.Message);
}
}
}
In this example, we create an EventLogQuery
object with the log name "Microsoft-Windows-UniversalTelemetryClient/Operational" and the query "*" to retrieve all events from the log. We then create an EventLogReader
object to read events from the query. The ReadEvent()
method is used to retrieve each event record, and we can access various properties of the EventRecord
object, such as the event ID, time, level, and message.
Please note that querying event logs requires administrative privileges, so make sure to run your application with appropriate permissions. Additionally, the log name and query can be modified as per your specific requirements to retrieve the desired event log information.