1. hism...@dot-solution.net Aug 16, 2:51 pm show options
Newsgroups: microsoft.public.vb.general.discussion
From: hism...@dot-solution.net - Find messages by this author
Date: 16 Aug 2005 11:51:51 -0700
Local: Tues, Aug 16 2005 2:51 pm
Subject: Query Eventlog by Timewritten field.
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse
I am creating a vb.net service that will run AS SERVICE. There is
actually a timer; so the timer will run every 2 minutes and see if
there are any new ERROR type logs in the APPLICATION logs. And if
there are then it will send an email to me ( or whoever I want to
specify). The thing that I can not figure out is how do I create the
query so it queries only thing in the past 2 minutes. If I take the
TimeWritten out of my query it works fine but pulls are the ERROR logs
from APPLICATION logs. This was orginally created in VBSCRIPT but I
have to write it in VB.NET. Here is the code:
Dim dtmStart, SearchEventStart
Dim objWMIService, colEvents, objEvent, EventTime
Dim objMessage, eventCounted, objRecordset, dtDate, dtHour
'
------------------------------*------------------------------*----------
Const eMailAddress = "hashim.ism...@fac.com" ' must be a valid
eMail address!!
Const MinutesToSearchWithin = -2 ' look for the event that
triggered me within the last 2 minutes
objMessage = CreateObject("CDO.Message")
'objMessage.Sender = "NTAdm...@fac.com"
objMessage.To = eMailAddress
dtmStart = CreateObject("WbemScripting.SW*bemDateTime")
SearchEventStart = DateAdd("n", MinutesToSearchWithin, Now())
dtmStart.SetVarDate(SearchEven*tStart, True)
MsgBox(dtmStart, MsgBoxStyle.Information)
objWMIService = GetObject("winmgmts:" _
&
"{impersonationLevel=impersona*te}!\\.\root\cimv2" )
colEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile =
'Application' AND type = 'ERROR' AND TimeWritten >= '" & dtmStart &
"'")
' Because we go through all recent events, in case of multiple
events within the last 2 minutes, we might mis
' events. That is: within multiple events that occur within the
last 2 minutes, not for all these events an eMail message
' may be generated. This is a trade-off to avoid generating
multiple eMail messages for one particular event within a
' series of events that occured within the last 2 minutes.
For Each objEvent In colEvents
'On Error Resume Next ' Maybe you should uncomment this
line...
objMessage.TextBody = "Category: " & objEvent.Category
EventTime = Mid(objEvent.TimeWritten, 5, 2) & "/" &
Mid(objEvent.TimeWritten, 7, 2) & "/" & _
Mid(objEvent.TimeWritten, 1, 4) & " " &
Mid(objEvent.TimeWritten, 9, 2) & ":" & _
Mid(objEvent.TimeWritten, 11, 2) & "." &
Mid(objEvent.TimeWritten, 13, 2)
objMessage.From = "NTAd...@fac.com"
objMessage.TextBody = objMessage.TextBody & vbCrLf & "Time:
" & EventTime
objMessage.Subject = "ERROR Event in " &
objEvent.SourceName & " on " & objEvent.ComputerName & " at " &
EventTime
objMessage.TextBody = objMessage.TextBody & vbCrLf &
"EventID: " & objEvent.EventCode
objMessage.TextBody = objMessage.TextBody & vbCrLf &
"Description: " & objEvent.Message
objMessage.TextBody = objMessage.TextBody & vbCrLf &
"RecordNumber: " & objEvent.RecordNumber
objMessage.TextBody = objMessage.TextBody & vbCrLf &
"SourceName: " & objEvent.SourceName
objMessage.TextBody = objMessage.TextBody & vbCrLf & "Type:
" & objEvent.Type
objMessage.TextBody = objMessage.TextBody & vbCrLf & "User:
" & objEvent.User
objMessage.Send()
Next