2021年3月28日星期日

How to delete event log source with EventRecordID using c#

I have tried with the following code to delete event log source. Referred with the document https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.eventlog.delete?view=net-5.0

                    string applicationlog = "Application";                      string mymachine = System.Environment.MachineName;                       EventLog myapplicationLog = new EventLog(applicationlog,                         mymachine);                      EventLogEntryCollection entries =                         myapplicationLog.Entries;                      int LogCount = entries.Count;                      if (LogCount <= 0)                      {                          _logger.LogInformation("No Event Logs in the Log :");                      }                      if (EventLog.SourceExists("test123"))                      {                          // Find the log associated with this source.                           logName = EventLog.LogNameFromSourceName("test123", mymachine);                          // Make sure the source is in the log we believe it to be in.                          if (logName != "Application")                              return;                          // Delete the source and the log.                          EventLog.DeleteEventSource("test123", mymachine);                          EventLog.Delete(logName, mymachine);                                                 _logger.LogInformation(logName + " deleted.");                      }  

When i executing the above code, all events deleted. i need to delete specifically. How to delete event log with specific id(EventRecordID)?

https://stackoverflow.com/questions/66815903/how-to-delete-event-log-source-with-eventrecordid-using-c-sharp March 26, 2021 at 07:18PM

没有评论:

发表评论