Logging

J

Jeff

I have a program that watches folders for any
modifications and then writees to the Event Log. Problem
is , is that it writes it four (4) times. I will post the
code below and hopefully somone can point out the error
that I can't see.

Thanks for any help.


---- Code Below -----
Imports System.ServiceProcess
Imports System
Imports System.IO
Imports System.Diagnostics
Imports Microsoft.Win32


Public Class Service1
Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the
InitializeComponent() call

End Sub

'UserService overrides dispose to clean up the
component list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As
System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same
process. To add
' another service to this process, change the
following line to
' create a second service object. For example,
'
' ServicesToRun = New
System.ServiceProcess.ServiceBase () {New Service1, New
MySecondUserService}
'
ServicesToRun = New
System.ServiceProcess.ServiceBase() {New Service1()}

System.ServiceProcess.ServiceBase.Run
(ServicesToRun)
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the
Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Timers.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Timer1 = New System.Timers.Timer()
CType(Me.Timer1,
System.ComponentModel.ISupportInitialize).BeginInit()
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 10000
'
'Service1
'
Me.ServiceName = "Service1"
CType(Me.Timer1,
System.ComponentModel.ISupportInitialize).EndInit()

End Sub

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method
should set things
' in motion so your service can do its work.

'set this property to true to start watching
Timer1.Enabled = True

End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary
to stop your service.
Timer1.Enabled = False

End Sub

Private Sub logchange(ByVal source As Object, ByVal e
As System.IO.FileSystemEventArgs)

Dim sSource As String
Dim sLog As String
Dim sEvent As String
Dim strMessage As String
Dim objKey As RegistryKey
Dim strUser As String
Dim strComputer As String

objKey = Registry.LocalMachine.OpenSubKey _

("SYSTEM\CurrentControlSet\Control\ComputerName\ComputerNam
e" _
, False)

sSource = "FolderWatcher"
sLog = "System"

strUser = System.Environment.UserName
strComputer = Convert.ToString(objKey.GetValue _
("ComputerName", ""))

If e.ChangeType = IO.WatcherChangeTypes.Changed
Then

strMessage &= " File " & e.FullPath & _
" has been modified - " & Now() & " - Changed"

sEvent = strMessage

If Not EventLog.SourceExists(sSource) Then
EventLog.CreateEventSource(sSource, sLog)
End If

'EventLog.WriteEntry(sSource, sEvent)
EventLog.WriteEntry(sSource, sEvent,
EventLogEntryType.Warning, 234)

sEvent = ""

End If

If e.ChangeType = IO.WatcherChangeTypes.Created
Then

strMessage &= "File " & e.FullPath & _
" has been created - " & Now()

sEvent = strMessage

If Not EventLog.SourceExists(sSource) Then
EventLog.CreateEventSource(sSource, sLog)
End If

'EventLog.WriteEntry(sSource, sEvent)
EventLog.WriteEntry(sSource, sEvent,
EventLogEntryType.Warning, 234)

sEvent = ""

End If

If e.ChangeType = IO.WatcherChangeTypes.Deleted
Then

strMessage &= "File " & e.FullPath & _
" has been deleted - " & Now()

sEvent = strMessage

If Not EventLog.SourceExists(sSource) Then
EventLog.CreateEventSource(sSource, sLog)
End If

'EventLog.WriteEntry(sSource, sEvent)
EventLog.WriteEntry(sSource, sEvent,
EventLogEntryType.Warning, 234)

sEvent = ""

End If

End Sub

Public Sub logrename(ByVal source As Object, ByVal e
As System.IO.RenamedEventArgs)

Dim sSource As String
Dim sLog As String
Dim sEvent As String
Dim strMessage As String
Dim objKey As RegistryKey
Dim strUser As String
Dim strComputer As String

objKey = Registry.LocalMachine.OpenSubKey _

("SYSTEM\CurrentControlSet\Control\ComputerName\ComputerNam
e" _
, False)

sSource = "FolderWatcher"
sLog = "System"

strUser = System.Environment.UserName
strComputer = Convert.ToString(objKey.GetValue _
("ComputerName", ""))

strMessage &= "File " & e.OldName & _
" has been renamed to " & e.Name

sEvent = strMessage

If Not EventLog.SourceExists(sSource) Then
EventLog.CreateEventSource(sSource, sLog)
End If

'EventLog.WriteEntry(sSource, sEvent)
EventLog.WriteEntry(sSource, sEvent,
EventLogEntryType.Warning, 234)

sEvent = ""


End Sub

Private Sub Timer1_Elapsed(ByVal sender As
System.Object, ByVal e As System.Timers.ElapsedEventArgs)
Handles Timer1.Elapsed

Dim watchfolder As FileSystemWatcher
Dim exeWatcher As FileSystemWatcher
Dim batWatcher As FileSystemWatcher
Dim scrWatcher As FileSystemWatcher

Dim strComputer As String
Dim strUser As String
Dim strMessage As String

watchfolder = New System.IO.FileSystemWatcher()
exeWatcher = New System.IO.FileSystemWatcher()
batWatcher = New System.IO.FileSystemWatcher()
scrWatcher = New System.IO.FileSystemWatcher()

'this is the path we want to monitor
watchfolder.Path = "C:\"
exeWatcher.Path = "C:\"
batWatcher.Path = "C:\"
scrWatcher.Path = "C:\"

watchfolder.IncludeSubdirectories = True
exeWatcher.IncludeSubdirectories = True
batWatcher.IncludeSubdirectories = True
scrWatcher.IncludeSubdirectories = True

watchfolder.Filter = "*.cmd"
exeWatcher.Filter = "*.exe"
batWatcher.Filter = "*.bat"
scrWatcher.Filter = "*.scr"

'Add a list of Filters we want to specify
'make sure you use OR for each filter as we need
to all of those
watchfolder.NotifyFilter =
IO.NotifyFilters.DirectoryName
exeWatcher.NotifyFilter =
IO.NotifyFilters.DirectoryName
batWatcher.NotifyFilter =
IO.NotifyFilters.DirectoryName
scrWatcher.NotifyFilter =
IO.NotifyFilters.DirectoryName

watchfolder.NotifyFilter =
watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName

exeWatcher.NotifyFilter = watchfolder.NotifyFilter
Or _
IO.NotifyFilters.FileName

batWatcher.NotifyFilter = watchfolder.NotifyFilter
Or _
IO.NotifyFilters.FileName

scrWatcher.NotifyFilter = watchfolder.NotifyFilter
Or _
IO.NotifyFilters.FileName

watchfolder.NotifyFilter =
watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes

exeWatcher.NotifyFilter = watchfolder.NotifyFilter
Or _
IO.NotifyFilters.Attributes

batWatcher.NotifyFilter = watchfolder.NotifyFilter
Or _
IO.NotifyFilters.Attributes

scrWatcher.NotifyFilter = watchfolder.NotifyFilter
Or _
IO.NotifyFilters.Attributes

'add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler exeWatcher.Changed, AddressOf logchange
AddHandler batWatcher.Changed, AddressOf logchange
AddHandler scrWatcher.Changed, AddressOf logchange

AddHandler watchfolder.Created, AddressOf logchange
AddHandler exeWatcher.Created, AddressOf logchange
AddHandler batWatcher.Created, AddressOf logchange
AddHandler scrWatcher.Created, AddressOf logchange

AddHandler watchfolder.Deleted, AddressOf logchange
AddHandler exeWatcher.Deleted, AddressOf logchange
AddHandler batWatcher.Deleted, AddressOf logchange
AddHandler scrWatcher.Deleted, AddressOf logchange

'add the rename handler as the signature is
differnet
AddHandler watchfolder.Renamed, AddressOf logrename
AddHandler exeWatcher.Renamed, AddressOf logrename
AddHandler batWatcher.Renamed, AddressOf logrename
AddHandler scrWatcher.Renamed, AddressOf logrename

'set this property to true to start watching
watchfolder.EnableRaisingEvents = True
exeWatcher.EnableRaisingEvents = True
batWatcher.EnableRaisingEvents = True
scrWatcher.EnableRaisingEvents = True

watchfolder.EnableRaisingEvents = True
exeWatcher.EnableRaisingEvents = True
batWatcher.EnableRaisingEvents = True
scrWatcher.EnableRaisingEvents = True

End Sub
End Class


--- End Code ---
 
N

NetPointer

r u sure that the code is alright? u r using timer and filewatcher both.. is
that ok or there is some misunderstanding??
 
J

Jeff

That is the only way I could figure out to do it. I am
open to other suggestions if anyone has any.
 
N

NetPointer

I think all u need to do is to create a filewatcher in form load...and write
the code in the events oof file watcher...

regards/.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top