0

I'm receiving an error when running a Windows service in the background, but no errors are shown when debugging with an attached debugger.

  1. Although I added logging statements to each method and catch block to determine any errors or hits to catch block, the running service did not log any statements while running in the background.
  2. While debugging with the attached debugger and stepping through each line, all statements were logged and the process was successful without any errors being thrown.

I aim to identify the cause of the exception being thrown during runtime, but not while debugging. The service runs every 60 seconds to check if any data is queued for processing in the database, and if so, it executes.

I require information on the possible causes of the error being thrown, and it would also be helpful if someone could provide guidance on logging using NLog, as no statements were logged when the service was running in the background.

Thanks in Advance!!

Vasundhara
  • 13
  • 4
  • *"The service runs every 60 seconds"*. No it doesn't. It's a Windows service. It's running all the time. I'm going to guess that you're using a `Timer` with an `Interval` of 60 seconds and you're executing code from its `Elapsed` event handler. That event is raised on a `ThreadPool` thread, so that would be the invocation being referred to. If you put the body of that event handler inside a `try` block, you should be able to catch and log the actual exception. – jmcilhinney Apr 06 '23 at 04:17
  • At a guess, as it is a service, then it could well be a user permission problem, but with no details about the "errors" then it tends to be difficult to help! You really need to post more information and try to pinpoint where exactly the error is occurring by adding more and more logging until you find the problem. – jason.kaisersmith Apr 06 '23 at 04:31
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Apr 06 '23 at 09:21
  • Despite adding a try/catch block to the OnStart and OnStop method to catch exceptions, I was unable to track the problem. To further investigate the issue, I attempted to debug the start process by running the executable file and attaching a debugger to the process. Unfortunately, my attempts to log using Nlog were unsuccessful, likely due to the fact that it is a background process. I also tried logging messages to a text file, but this did not yield any results. Ultimately, I decided to add an event log to view from the event viewer, but this too proved to be ineffective. – Vasundhara Apr 08 '23 at 18:12
  • Is it possible that the issue is related to the timeout set in the database connection? The application queries the database every 60 seconds to check for any new data that needs to be processed. I attempted to troubleshoot the issue by logging the thread ID and found that there were two threads running - one for the main thread and the other for the service process thread. Could the problem be related to the threads? – Vasundhara Apr 08 '23 at 18:15
  • @jason.kaisersmith Thank you! Let me check on them. I am receiving an error message stating "Exception has been thrown by the target of an invocation." The strange thing is that the error does not seem to be originating from any particular source, and when I attempt to debug the code, it executes without any issues. I find it suspicious that it should not be possible to connect to the database without user permissions, yet the error message pertains to a query from the database. – Vasundhara Apr 08 '23 at 18:24

0 Answers0