How to find Date and Time from Event Viewer and insert it into database table in SQL-Server using C#?
Please suggest me is it possible through like- windows service or console application or windows form or api ?
Loading
How to find Date and Time from Event Viewer and insert it into database table in SQL-Server using C#?
Please suggest me is it possible through like- windows service or console application or windows form or api ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mahesh ChandRank #1 · 393.3K reputationPosted Jan 5, 2023, 9:02 PM EST
Answer generated by ChatGPT.
To find the date and time of an event in the Event Viewer using C#, you can use the
EventLogclass from theSystem.Diagnosticsnamespace. This class allows you to read events from event logs, including the system event log, on the local or a remote computer.Here is an example of how you can use the
EventLogclass to retrieve the date and time of an event:using System; using System.Diagnostics; EventLog log = new EventLog("System"); foreach (EventLogEntry entry in log.Entries) { // Check if the entry is the event you are looking for if (entry.Source == "MyEventSource" && entry.InstanceId == 12345) { Console.WriteLine("Date: " + entry.TimeGenerated); Console.WriteLine("Time: " + entry.TimeGenerated.TimeOfDay); } }Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 5, 2023, 11:36 AM EST
Please find the snippet to read the event viewer using C#.
https://stackoverflow.com/questions/3147972/read-event-log-in-c-sharp
It's up to your business use case, which way you want to consume event viewer. My suggestion is to create a separate library to read the event viewer, something like EventCollector which now will consume it from EventViewer, maybe tomorrow you are relaying on a different source. On top of that, you can switch to whichever way you want. may be console/win service/form/API.
hope this helps you