Advertisement
tankcr

getlogs

Jun 2nd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7.  
  8. namespace GetLogs
  9. {
  10.  
  11.     public class Eventlogs
  12.     {
  13.       string eventLog;
  14.         public string EventLog
  15.         {
  16.             get { return eventLog; }
  17.             set { eventLog = value; }
  18.         }
  19.  
  20.         public Eventlogs()
  21.         {
  22.         }
  23.     }
  24.         public class GetEvents
  25.         {
  26.         public static List<EventLog> GetEventLogs(string logtype, string computername)    
  27.         {
  28.                 //logType can be Application, Security, System or any other Custom Log.
  29.                 EventLog ev = new EventLog(logtype, computername);
  30.                 int LastLogToShow = ev.Entries.Count;
  31.                 if ( LastLogToShow <= 0 )
  32.                 Console.WriteLine("No Event Logs in the Log :" + logtype + computername);
  33.                 // Read the last 2 records in the specified log.
  34.                 int i;
  35.                 for ( i = ev.Entries.Count - 1; i>= LastLogToShow - 2; i--)
  36.                 {
  37.                     EventLogEntry CurrentEntry = ev.Entries[i];
  38.                     Console.WriteLine("Event ID : " + CurrentEntry.EventID);
  39.                     Console.WriteLine("Entry Type : " + CurrentEntry.EntryType.ToString());
  40.                     Console.WriteLine("Message :  " + CurrentEntry.Message + "\n");
  41.                 }  
  42.                 ev.Close();
  43.             }
  44.         }      
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement