Solved

[Vb.net] Eventlog Watcher Service becomes unresponsive after large amount of entries

Posted on 2013-01-07
6
705 Views
Last Modified: 2013-01-07
This is the situation:
I have written a Windows Service that monitors the Domain Controllers Security eventlog for certain events.
It filters the eventmessages and sends email if a filter is matched.

The service runs fine until our HR-->Active Directory import runs.
This adds ca. 2000 events to the Security eventlog within 2 seconds.
After this, the service still runs, but doesn't respond to new events anymore.
Hopefully someone can point me in the right direction to solve this

Imports System.Threading
Imports System.Net.Mail
Imports System.Security.Principal

Public Class EventLogWatcherService
    Private Shared signal As AutoResetEvent

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' This is where the service starts 

        'Start the Event Log Watcher Service as a new thread
        Dim workerThread = New Thread(AddressOf WatchEventLog)
        workerThread.Start()

        'Write start message to Eventlog
        WriteLogMessage("Service Started", EventLogEntryType.Information)
    End Sub

    Public Sub WatchEventLog()
        'Set new event
        signal = New AutoResetEvent(False)

        'Connect to the OS-Security Eventlog
        Dim logwatcherlog = New EventLog("Security", ".", "LogMonitoringService")
        logwatcherlog.Source = "LogMonitoringService"

        'Add event handler to the Security Eventlog. This triggers at new events
        AddHandler logwatcherlog.EntryWritten, New System.Diagnostics.EntryWrittenEventHandler(AddressOf EventLog1_EntryWritten)
        logwatcherlog.EnableRaisingEvents = True

        'Wait for Event
        signal.WaitOne()
    End Sub

    Public Sub EventLog1_EntryWritten(ByVal [source] As Object, ByVal e As entryWrittenEventArgs)
        'Check the Events on EventID's. On match a new thread is fired to handle this event.
        If e.Entry.InstanceId = "4728" Or e.Entry.InstanceId = "4729" Then
            Dim thread As New Threading.Thread(AddressOf FoundEventToHandle)
            thread.Start(e)
        End If
    End Sub

    Public Sub FoundEventToHandle(ByVal e As EntryWrittenEventArgs)
            ‘Filters the eventmessage and sends an email on a filter match.

            '** WHEN HR-->AD IMPORT IS RUN, THIS PART ISN"T REACHED**
            
            'Dispose the current Entry to prevent unresponive service
            e.Entry.Dispose()

           'Sleep short and Signal for complete
           Thread.Sleep(1)
           signal.Set()
    End Sub

Open in new window

0
Comment
Question by:orbisict
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38750423
I think it happens because you try to create 2000 threads within 2 seconds which overwhelms it. What you can do is change the approach and use a queue with a single thread. Whenever a new entry is written, you put the EntryWrittenEventArgs into a queue. A single thread can then keep monitoring this queue and process the items when found.
0
 
LVL 1

Author Comment

by:orbisict
ID: 38750849
Thanks @CodeCruiser, That was the solution !
I have it running already.

Just to be complete, here is part of the code that I changed:

In the Public Class, I added
Public EntryCollection As New Collection

Open in new window


in the Onstart I added a new thread to watch the eventlog queue
Dim EventCollectionWatcheTread = New Thread(AddressOf EventCollectionWatcher)
EventCollectionWatcheTread.start()
 

Open in new window


The Eventlog1 sub, where I handled the filter, now just looks like this:
Public Sub EventLog1_EntryWritten(ByVal [source] As Object, ByVal e As EntryWrittenEventArgs)
        'Add new entries to queue
        EntryCollection.Add(e)
        signal.Set()  
End Sub

Open in new window


The new Thread that watches the collection looks like this:
Public Sub EventCollectionWatcher()
       Do
            If EntryCollection.Count > 0 Then
                Dim e As EntryWrittenEventArgs = EntryCollection(1)
                If e.Entry.InstanceId = "4728" Or e.Entry.InstanceId = "4729" Then
                    Dim thread As New Threading.Thread(AddressOf FoundEventToHandle)
                    thread.Start(e)
                End If
                EntryCollection.Remove(1)
            End If
            Thread.Sleep(100)
        Loop
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:orbisict
ID: 38750850
Exact description of the solution !
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38751774
By the way, you are still creating extra threads in EventCollectionWatcher method which you don't need to as this method is already executing on a separate thread so you can put the code from FoundEventToHandle method in there.
0
 
LVL 1

Author Comment

by:orbisict
ID: 38752135
Thanks CodeCruiser, I'm new to thread programming, therefore I really appreciate your assistance !
The 'FoundEventToHandle' sub is rather large and I'd like to leave it as a seperate sub.

Would it be ok to handle the event as shown below ?
Is it also correct to assume that I can delete the line  'Thread.Sleep(100)' too, because the code is now running as a single thread ?

Thanks !

Public Sub EventCollectionWatcher()
       Do
            If EntryCollection.Count > 0 Then
                Dim e As EntryWrittenEventArgs = EntryCollection(1)
                If e.Entry.InstanceId = "4728" Or e.Entry.InstanceId = "4729" Then
                       FoundEventToHandle()
                End If
                EntryCollection.Remove(1)
            End If
         Loop
End Sub

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38753030
Yeah this should be fine.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question