1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
Module EASMonitoring
Private watcher AsManagementEventWatcher
PrivateWithEvents tMonitor As System.Timers.Timer
Sub Main(ByVal args() AsString)
Try
tMonitor = New Timers.Timer(5000)
tMonitor.Enabled = True
AddHandler tMonitor.Elapsed, AddressOf tMonitor_Elapsed
StartListen()
Catch ex AsException
Console.WriteLine(ex.Message & vbNewLine & ex.StackTrace)
EndTry
EndSub
PublicSub NewEvent(ByVal sender AsObject, ByVal e AsEventArrivedEventArgs)
Try
Dim dateN AsNewDate
dateN = DateTime.Now
IfCType(e.NewEvent("TargetInstance"), ManagementBaseObject)("Message").ToString.Contains("Task") Then
IO.File.AppendAllText(My.Application.Info.DirectoryPath & "\Monitor_" & dateN.ToString("yyyy_MM_dd") & ".log", ((CType(e.NewEvent("TargetInstance"), ManagementBaseObject))("Message")) & vbNewLine, System.Text.Encoding.UTF8)
EndIf
Catch ex AsException
Console.WriteLine(ex.Message & vbNewLine & ex.StackTrace)
EndTry
EndSub
PublicSub StartListen()
Try
Console.WriteLine("Activation du processus d'écoute...")
watcher = New System.Management.ManagementEventWatcher
watcher.Query = New System.Management.EventQuery("SELECT * FROM __InstanceCreationEvent WHERE TargetInstance ISA ""Win32_NTLogEvent"" and TargetInstance.LogFile = ""Application""")
AddHandler watcher.EventArrived, AddressOf NewEvent
watcher.Start()
Catch ex AsException
Console.WriteLine(ex.Message & vbNewLine & ex.StackTrace)
EndTry
EndSub
PublicSub StopListen()
Try
Console.WriteLine("Arrêt du processus d'écoute...")
watcher.Stop()
Catch ex AsException
Console.WriteLine(ex.Message & vbNewLine & ex.StackTrace)
EndTry
EndSub
PrivateSub tMonitor_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tMonitorEAS.Elapsed
Try
Dim dateN AsDate = DateTime.Now
IfFile.Exists(My.Application.Info.DirectoryPath & "\Monitor_" & dateN.ToString("yyyy_MM_dd") & ".log") Then
'Traitement...
'...
File.Delete(My.Application.Info.DirectoryPath & "\Monitor_" & dateN.ToString("yyyy_MM_dd") & ".log")
EndIf
Catch ex AsException
Console.WriteLine(ex.Message & vbNewLine & ex.StackTrace)
EndTry
EndSub
EndModule |
Partager