How can i save event logs using C# ?
-
Hi, Is it possible to save event logs using C# ? Any idea ? Thansk in advance !!
cheers, Abhijit
-
Hi, Is it possible to save event logs using C# ? Any idea ? Thansk in advance !!
cheers, Abhijit
You could use the EventLog object to read the event log entries and then save them to a database or file, like so:
// Opens the Application event log.
EventLog eventLog = new EventLog("Application");
// Iterate through the entries in the opened event log.
foreach (EventLogEntry entry in eventLog.Entries)
Console.WriteLine(entry.Message);/F - .NET Developer
modified on Wednesday, July 30, 2008 10:23 AM
-
You could use the EventLog object to read the event log entries and then save them to a database or file, like so:
// Opens the Application event log.
EventLog eventLog = new EventLog("Application");
// Iterate through the entries in the opened event log.
foreach (EventLogEntry entry in eventLog.Entries)
Console.WriteLine(entry.Message);/F - .NET Developer
modified on Wednesday, July 30, 2008 10:23 AM
Use Microsoft Enterprise Library for Event Log, Flat file logging or Error Logging.
-
You could use the EventLog object to read the event log entries and then save them to a database or file, like so:
// Opens the Application event log.
EventLog eventLog = new EventLog("Application");
// Iterate through the entries in the opened event log.
foreach (EventLogEntry entry in eventLog.Entries)
Console.WriteLine(entry.Message);/F - .NET Developer
modified on Wednesday, July 30, 2008 10:23 AM
Thanks for your response. but i want to save it ".evt" file.
cheers, Abhijit
-
Thanks for your response. but i want to save it ".evt" file.
cheers, Abhijit
I don't believe the .NET Framework has native support for this, but you can try the following:
// Declare this at the class member space
[DllImport("advapi32.dll")]
static extern IntPtr OpenEventLog(string lpUNCServerName, string lpSourceName);
[DllImport("advapi32.dll")]
static extern bool BackupEventLog(IntPtr hEventLog, string backupFile);// Somewhere in your code, call the above two declarations...
// NOTE: Replace "localhost" with the machine name you need to back up.
// Replace "Application" with the log you are trying to back up.
// The saved file is an EVT file.
BackupEventLog(OpenEventLog("localhost", "Application"), @"C:\TEMP\EVENTLOG.EVT");/F - .NET Developer
-
I don't believe the .NET Framework has native support for this, but you can try the following:
// Declare this at the class member space
[DllImport("advapi32.dll")]
static extern IntPtr OpenEventLog(string lpUNCServerName, string lpSourceName);
[DllImport("advapi32.dll")]
static extern bool BackupEventLog(IntPtr hEventLog, string backupFile);// Somewhere in your code, call the above two declarations...
// NOTE: Replace "localhost" with the machine name you need to back up.
// Replace "Application" with the log you are trying to back up.
// The saved file is an EVT file.
BackupEventLog(OpenEventLog("localhost", "Application"), @"C:\TEMP\EVENTLOG.EVT");/F - .NET Developer
Thanks for your reply. I am trying to do it using WMI
cheers, Abhijit