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

时间:2016-01-01 14:59:20

标签: c# .net vb.net service windows-services

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

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

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

  

安装服务......

     

服务已成功安装。

     

在日志应用程序中创建EventLog源...

我不确定这些类的哪个成员要求打印该信息的方式,只是可能禁用那些不受欢迎的详细信息?

1 个答案:

答案 0 :(得分:2)

在实例化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