Properties Window from the menu).

问题描述 投票:0回答:1
In the Properties grid, set the

AutoLog property to . This will prevent events from being written by default to the Windows Application log .

If you have not already done so, right-click anywhere in the [Design] view of the service component, and select the ServiceInstallerAdd Installer

menu option. This adds a component to your solution named

by default.

Note that the new ServiceBase.EventLog.Log is automatically opened to its respective [Design] view. To get to its code, right-click the ProjectInstaller.cs file in the Solution Explorer, and select the View Code menu option.EventLog.LogChange the contents of that file to look like the following. This updates the

to use the custom log name of your choice.

enter image description here

The last step is to tie the service component to the custom event log you named in Step 6. To do that, right-click the service component in the Solution Explorer, and select the View Code menu option. Update the constructor to the following:ServiceInstallerWhen you install the service, the

YourEventLogName

should be created on the system. If the Event Viewer is already open, you'll probably have to refresh it. If it's still not visible, you might need to log something to it (I don't recall the details). In any case, to write log information to the custom event log from your service, use the
c# windows-services event-log custom-eventlog
1个回答
0
投票

我目前正在创建一个Windows服务,刚刚发现(感谢 这个 答)如何配置服务安装程序,以便在安装过程中创建一个自定义事件日志源。我自己已经弄清楚了,这些自定义事件日志源需要高权限才能注册。这就是为什么在安装过程中发生注册的原因--因为服务安装总是以高权限执行。到目前为止,还算不错。

  1. 然而,我对这个解决方案并不是100%满意,因为,就像文档中所说的 ServiceBase:
  2. 这个源的Log属性被ServiceInstaller构造函数设置为计算机的Application日志。但这不是我想要的。我希望事件被登记在一个名为 "MyCustomLog "的自定义日志中。此外,我不能仅仅将我的服务的 为 "MyCustomLog"。如何单独设置我的服务的 false? 我应该在哪里做呢?
  3. 因为我还没有找到问题的答案,所以我想到了为我的服务事件创建一个自定义视图,它应该像下面这样。它并不能取代自定义事件日志,因为事件仍然登记在应用日志中,但它能让我对服务中发生的某些事件有一个概览,就像在自定义事件日志中一样。那么,我如何以编程方式创建这样的自定义视图?这可能吗?如果可以,我需要在哪里创建它们?创建是否需要提升权限,所以需要在 ? 还是可以在我的服务构造函数中轻松完成?ProjectInstaller我希望得到关于这两种方法的可行性的答案。
  4. ProjectInstaller
  5. 我目前正在创建一个Windows服务,并且刚刚弄清楚(感谢这个答案)如何配置服务安装程序,以便在安装过程中创建一个自定义事件日志源。由于我...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. 此处
    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. . 在Visual Studio 2017和Visual Studio 2019中,以下内容应该可以最小化地工作。在Visual Studio的 "解决方案资源管理器 "中,双击您的服务组件,即派生的组件。. 这样做将在[设计]视图中打开组件。EventLog打开[设计]视图后,按F4(或View
© www.soinside.com 2019 - 2024. All rights reserved.