Question/Suggestions on using a Windows Service or Hangfire

muttBunch 100 Reputation points
2024-01-06T23:27:13.3966667+00:00

I plan on either doing a Windows Service, or using Hangfire (right in my web api), for continuously gathering new security event logs similar to that of the Windows Event Viewer.

Looking for feedback/suggestions on doing it with an event listener.

What would happen if I ran a continuous while loop for the life of the service using the following:

using EventLogWatcher logWatcher = new EventLogWatcher(query);

logWatcher.EventRecordWritten += new EventHandler<EventRecordWrittenEventArgs>((sender, e) => LogWatcher_EventRecordWritten(sender, e, domain));

try 
{	
	while (!token.IsCancellationRequested)
	{
		logWatcher.Enabled = true;
	}
}
...

Would this potentially, ever, lead to a memory leak if it runs constantly, for the life of the service running?

Any recommendations on how this should really be handled, please let me know.

Thanks

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,614 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
343 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,706 Reputation points
    2024-01-07T18:39:32.8766667+00:00

    No, but the code

        while (!token.IsCancellationRequested)
    	{
    		logWatcher.Enabled = true;
    	}
    

    Is compute bound and will 100% of the cpu. Also there is no need to keep resetting the enabled. And where is the disable code? Try:

        logWatcher.Enabled = true;
        while (!token.IsCancellationRequested)
    	{
            Thread.Sleep(500);
    	}
        logWatcher.Enabled = false;
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.