Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read file then email, but dont email if no new errors!

Status
Not open for further replies.

honeypot

Technical User
Mar 6, 2001
147
GB
Hi there :) ive managed to muddle through and write a small program that reads a particular log file, checks for the word "error" and then sends an email notification. At the moment the program sends an email even if there are no new errors & i dont want it to send blank emails. Could some1 pls tell me where ive gone wrong? here is my code:

Imports System
Imports System.IO
Imports System.Collections
Imports System.Web


Module Module1


Sub Main()

Dim myTimer As New System.Timers.Timer
Dim LineNo As String
myTimer.Stop()
Dim CurrentLineNo As New Integer
If GetSetting("OrEM", "Settings", "LineNo") = "" Then
SaveSetting("OrEM", "Settings", "LineNo", "1")
End If
LineNo = Val(GetSetting("OrEM", "Settings", "LineNo"))
Dim objReader As New StreamReader("filename")
Dim sLine As String = ""
Dim arrText As New ArrayList

Do
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
If CurrentLineNo >= LineNo Then
If InStr(UCase(sLine), "MON") Or InStr(UCase(sLine), "TUE") Or InStr(UCase(sLine), "WED") Or InStr(UCase(sLine), "THU") Or InStr(UCase(sLine), "FRI") Or InStr(UCase(sLine), "SAT") Or InStr(UCase(sLine), "SAT") Then
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1
If InStr(UCase(sLine), "ERROR") Then arrText.Add(sLine)
While sLine <> &quot;&quot;
sLine = objReader.ReadLine() : CurrentLineNo = CurrentLineNo + 1

End While
End If
End If
If Not sLine Is Nothing Then

End If
Loop Until sLine Is Nothing
objReader.Close()

If sLine <> &quot;&quot; Then
'create mail notification
Dim mailMsg As New System.Web.Mail.MailMessage
mailMsg.BodyFormat = Mail.MailFormat.Text
mailMsg.To = &quot;me@co.uk&quot;
mailMsg.Subject = &quot;name&quot;
mailMsg.From = &quot;name&quot;

Dim txtBody As String

For Each sLine In arrText
txtBody = txtBody & sLine & vbCrLf
Next

mailMsg.Body = txtBody
System.Web.Mail.SmtpMail.SmtpServer = &quot;server&quot;
System.Web.Mail.SmtpMail.Send(mailMsg)
End If
SaveSetting(&quot;OrEm&quot;, &quot;Settings&quot;, &quot;LineNo&quot;, Trim(Str(CurrentLineNo - 2)))
LineNo = Val(GetSetting(&quot;OrEM&quot;, &quot;Settings&quot;, &quot;LineNo&quot;))

CurrentLineNo = 0
myTimer.Interval = 30000
myTimer.Start()
End Sub

End Module

It hasn't worked since i added the (if sline <> &quot;&quot; then.......end if) which is wrapped around the mail code?

Thanx - any help much appreciated!
 
sLine will be Nothing if no error lines were found so you should use:

If Not sLine Is Nothing then

instead of

If sLine <> &quot;&quot; then

&quot;&quot; is a null string not Nothing

You almost have the code correct. There is a test for Not sLine Is Nothing inside the main loop. Just move your email notification code to that place.
 
ok - will try that - thank u very much :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top