Windows 服务始终写入自定义日志和应用程序日志

发布于 2024-09-15 23:57:17 字数 1058 浏览 0 评论 0原文

我正在为我的 Windows 服务使用自定义 EventLog。该服务在安装后创建事件源。我没有任何问题。

不过,我已经设置了我的服务,以便我可以使用以下机制从 IDE 运行它:

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);
    }
}

写入事件日志时,它似乎会写入我的自定义事件日志和应用程序日志。我怎样才能防止这种情况发生?

下面是我用来写入事件日志的代码:(EventLog 应用程序设置的源和名称相同)

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

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌化 2024-09-22 23:57:17

看来重新启动我的机器已经解决了这个问题。我还不确定为什么,但我假设事件查看器机制进入了某种奇怪的状态。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文