以编程方式安装服务而不打印调试信息

Programmatically install a service without printing debug info

我正在使用此代码段以编程方式安装服务:

C#:

public static void InstallService(string filepath, 
                                 string svcName, 
                                 string displayName = "", 
                                 string description = "", 
                                 ServiceStartMode startType = ServiceStartMode.Automatic,
                                 ServiceAccount account = ServiceAccount.LocalSystem, 
                                 string username = "", 
                                 string password = "")
{
    using (ServiceProcessInstaller installer = new ServiceProcessInstaller()) {

        using (ServiceInstaller svc = new ServiceInstaller()) {

            InstallContext context = new InstallContext("", { string.Format("/assemblypath={0}", filepath) });

            installer.Account = account;
            installer.Username = username;
            installer.Password = password;

            svc.Context = context;
            svc.DisplayName = displayName;
            svc.Description = description;
            svc.ServiceName = svcName;
            svc.StartType = startType;
            svc.Parent = installer;

            ListDictionary state = new ListDictionary();
            svc.Install(state);
            state.Clear();

        }

    }

}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================

Vb.Net(原文):

Public Shared Sub InstallService(ByVal filepath As String,
                                 ByVal svcName As String,
                                 Optional ByVal displayName As String = "",
                                 Optional ByVal description As String = "",
                                 Optional ByVal startType As ServiceStartMode = ServiceStartMode.Automatic,
                                 Optional ByVal account As ServiceAccount = ServiceAccount.LocalSystem,
                                 Optional ByVal username As String = "",
                                 Optional ByVal password As String = "")

    Using installer As New ServiceProcessInstaller

        Using svc As New ServiceInstaller

            Dim context As New InstallContext("", {String.Format("/assemblypath={0}", filepath)})

            installer.Account = account
            installer.Username = username
            installer.Password = password

            svc.Context = context
            svc.DisplayName = displayName
            svc.Description = description
            svc.ServiceName = svcName
            svc.StartType = startType
            svc.Parent = installer

            Dim state As New ListDictionary
            svc.Install(state)
            state.Clear()

        End Using

    End Using

End Sub

问题是在调用该方法时,安装状态写在应用程序的调试控制台中:

Installing service...

Service has been successfully installed.

Creating EventLog source in log Application...

我不确定 类 中的哪些成员以何种方式请求打印该信息,只是它可能会禁用不需要的冗长内容吗?

在实例化 InstallContext 时使用 LogToConsole=false 命令行参数,如下所示...

InstallContext context = new InstallContext("", { string.Format("/assemblypath={0} /LogToConsole=false", filepath) });

参考文献:

https://msdn.microsoft.com/en-us/library/system.configuration.install.installcontext_properties%28v=vs.110%29.aspx

https://msdn.microsoft.com/en-us/library/50614e95%28v=vs.110%29.aspx