0

我目前正在创建 Windows 服务,并且刚刚弄清楚(感谢这个答案)如何配置服务安装程序,以便在安装期间创建自定义事件日志源。正如我自己已经弄清楚的那样,这些自定义事件日志源需要注册提升的权限。这就是在安装过程中发生注册的原因 - 因为服务安装始终以提升的权限执行。到目前为止,一切都很好。

但是,我对该解决方案不是 100% 满意,因为正如以下文档中所述ServiceInstaller

此源的 Log 属性由 ServiceInstaller 构造函数设置为计算机的应用程序日志。

这不是我想要的。我希望将事件注册在名为“MyCustomLog”的自定义日志中。此外,我不能只将我的服务设置ServiceBase.EventLog.Log为“MyCustomLog”。如何单独设置我的服务EventLog.Log?我在哪里必须这样做?

由于我还没有找到我的问题的答案,所以我想为我的服务事件创建一个自定义视图,该视图应该如下所示:

在此处输入图像描述

它不会取代自定义事件日志,因为事件仍注册到应用程序日志中,但它使我能够概览我的服务中发生的某些事件,就像它在自定义事件日志中一样。那么,我如何以编程方式创建这样的自定义视图?那可能吗?如果是这样,我需要在哪里创建它们?创建是否需要提升权限,因此需要在ServiceInstaller? 或者这可以在我的服务的构造函数中轻松完成吗?

我将不胜感激有关这两种方法的可行性的答案!

4

1 回答 1

1

更多细节在这里。以下内容应至少在 Visual Studio 2017 和 Visual Studio 2019 中有效。

  1. 在 Visual Studio 的解决方案资源管理器中,双击您的服务组件,即派生自ServiceBase. 这样做会在 [Design] 视图中打开组件。
  2. 打开 [Design] 视图后,按 F4(或菜单中的 View|Properties Window)打开 Properties 网格。
  3. 在“属性”网格中,将AutoLog属性设置为false。这将阻止默认情况下将事件写入 Windows 应用程序日志。
  4. 如果您还没有这样做,请右键单击服务组件的 [Design] 视图中的任意位置,然后选择Add Installer菜单选项。ProjectInstaller这会在您的解决方案中添加一个默认命名的组件。
  5. 请注意,新ProjectInstaller的会自动打开到其各自的 [设计] 视图。要查看其代码,请在解决方案资源管理器中右键单击 ProjectInstaller.cs 文件,然后选择查看代码菜单选项。
  6. 将该文件的内容更改为如下所示。这将更新EventLogInstaller以使用您选择的自定义日志名称。
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Diagnostics;

    namespace YourProjectNamespace
    {
        [RunInstaller(true)]
        public partial class ProjectInstaller : Installer
        {
            public ProjectInstaller()
            {
                InitializeComponent();

                EventLogInstaller installer = FindInstaller(this.Installers);
                if (installer != null)
                {
                    installer.Log = "YourEventLogName"; // enter your event log name here
                }
            }

            private EventLogInstaller FindInstaller(InstallerCollection installers)
            {
                foreach (Installer installer in installers)
                {
                    if (installer is EventLogInstaller)
                    {
                        return (EventLogInstaller)installer;
                    }

                    EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers);
                    if (eventLogInstaller != null)
                    {
                        return eventLogInstaller;
                    }
                }
                return null;
            }
        }
    }
  1. 最后一步是将服务组件绑定到您在步骤 6 中命名的自定义事件日志。为此,请在解决方案资源管理器中右键单击服务组件,然后选择查看代码菜单选项。将构造函数更新为以下内容:
    public YourServiceName()
    {
        InitializeComponent();

        // This ties the EventLog member of the ServiceBase base class to the
        // YourEventLogName event log created when the service was installed.
        EventLog.Log = "YourEventLogName";
    }
  1. 安装服务时,应在系统上创建YourEventLogName 。如果事件查看器已经打开,您可能需要刷新它。如果它仍然不可见,您可能需要在其中记录一些内容(我不记得详细信息)。在任何情况下,要将日志信息从您的服务写入自定义事件日志,请使用EventLog服务组件的成员,例如,
    protected override void OnStart(string[] args)
    {
        EventLog.WriteEntry("The service was started successfully.", EventLogEntryType.Information);
    }

    protected override void OnStop()
    {
        EventLog.WriteEntry("The service was stopped successfully.", EventLogEntryType.Information);
    }

    protected override void OnShutdown()
    {
        EventLog.WriteEntry("The service was shutdown successfully", EventLogEntryType.Information);
    }
于 2020-05-02T21:38:11.607 回答