Question

I have written a custom Windows Service that writes data to a custom Event Log (in the Windows Event Viewer).

For dev'ing the biz logic that the service uses, I created a Windows Form which simulates the Start/Stop methods of the Windows Service.

When executing the biz logic via the Windows Forms, info is successfully written to my custom Event Log. However, when I run the same biz logic from the custom Windows Service, information is failing to be written to the Event Log.

To be clear, I have written a library (.dll) that does all the work that I want my custom service to do - including the create/write to the custom Event Log. My Form application references this library as does my Windows Service.

Thinking the problem is a security issue, I manually set the custom Windows Service to "Log on" as "Administrator", but the service still did not write to the Event Log.

I'm stuck on how to even troubleshoot this problem since I can't debug and step into the code when I run the service (if there is a way to debug a service, please share).

Do you have any ideas as to what could be causing my service to fail to write to the event log?

Was it helpful?

Solution 3

I discovered why my service wasn't writing to the Event Log.

The problem had nothing to do with any part of the code/security/etc that was attempting to write to the EL. The problem was that my service wasn't successfully collecting the information that is written to the EL - therefore, the service wasn't even attempting to write the log.

Now that I fixed the code that collects the data, data is successfully writing to the event log.

I'm open to having this question closed since the question was amiss to the real problem.

OTHER TIPS

I use it like this. There can be some typos. Writed it on my phone browser...

public class MyClass
{
   private EventLog eventLog = new EventLog();

   public void MyClass()
   {
      if (!System.Diagnostics.EventLog.SourceExists("MyLogSource"))
          System.Diagnostics.EventLog.CreateEventSource("MyLogSource", "MyLogSource_Log");
      eventLog.Source = "MyLogSource";
      eventLog.Log = "MyLogSource_Log";
   }

   private void MyLogWrite()
   {
       eventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error);
   } 

}

To debug a running service you need to attach to the process. See here for the steps.

You could also add parameter checking to the Main entry point and have a combination service and console app which would start based on some flag. See this SO post for a good example but here's a snippet:

using System;
using System.ServiceProcess;

namespace WindowsService1
{
    static class Program
    {
        static void Main(string[] args)
        {
            if (args == null)
            {
                Console.WriteLine("Starting service...");
                ServiceBase.Run(new ServiceBase[] { new Service1() });
            }
            else
            {
                Console.WriteLine("Hi, not from service: " + args[0]);
            }
        }
    }
}

The above starts the app in console mode if there any parameters exist and in service mode if there are no parameters. Of course it can be much fancier but that's the gist of the switch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top