Windows service always writes to custom log and ap

2019-05-28 10:46发布

问题:

I am using a custom EventLog for my Windows service. The service creates the event source after installtion. I don't have any problems.

However, I have setup my service so that I can run it from the IDE using the following mechanism:

static void Main(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);

    string firstArgument = string.Empty;

    if (args.Length > 0)
        firstArgument = args[0].ToUpperInvariant();

    if (string.Compare(firstArgument, "-CONSOLE", true) == 0)
    {
        new SchedulerService().RunConsole(args);
    }
    else
    {
        ServiceBase[] services = new ServiceBase[] { new SchedulerService() };
        ServiceBase.Run(services);
    }
}

When writing to the event log, it seems to write my custom event log AND the application log. How can I prevent this from occurring?

Below is the code I am using to write to the event log: (The EventLog app setting is the same for the source and name)

using (System.Diagnostics.EventLog eventLog = 
   new EventLog(
      System.Configuration.ConfigurationManager.AppSettings["EventLog"], ".", 
      System.Configuration.ConfigurationManager.AppSettings["EventLog"]))
{
    eventLog.WriteEntry(msg, entryType);
}

回答1:

It seems that a reboot of my machine has fixed this problem. I am not sure why yet, but I am going to assume the Event Viewer mechanism got in to some kind of weird state.