【问题标题】:Self installing Windows Service leave install logs behind. How to disable this?自安装 Windows 服务留下安装日志。如何禁用此功能?
【发布时间】:2017-06-11 17:06:21
【问题描述】:

我正在使用以下代码自行安装 Windows 服务(此代码位于 Program.cs 中)。当我使用“servicename.exe --install”时,服务安装得很好,但是留下了一些安装日志。服务名称.InstallLog 和 InstallUtil.InstallLog。有没有办法完全禁用这些文件的日志记录?

if (Environment.UserInteractive)
        {
            string parameter = string.Concat(args);
            switch (parameter)
            {
                case "--install":
                    ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                    break;
                case "--uninstall":
                    ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                    break;
            }
        }
        else
        {
            ServiceBase[] servicesToRun = new ServiceBase[]
            {
         new Service1()
            };
            ServiceBase.Run(servicesToRun);
        }

【问题讨论】:

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


    【解决方案1】:

    ManagedInstallerClass.InstallHelper 处理 Installutil.exe(安装程序工具)的功能(请参阅https://msdn.microsoft.com/en-us/library/system.configuration.install.managedinstallerclass(v=vs.110).aspx)。根据文档 (https://docs.microsoft.com/en-us/dotnet/framework/tools/installutil-exe-installer-tool),InstallUtil 接受参数来重定向或禁用日志

    您可以尝试像这样更改InstallHelper 参数以禁用日志。似乎工作正常:

    string parameter = string.Concat(args);
    switch (parameter)
    {
        case "--install":
            ManagedInstallerClass.InstallHelper(new[] { "/LogFile=", "/LogToConsole=true", Assembly.GetExecutingAssembly().Location});
            break;
        case "--uninstall":
            ManagedInstallerClass.InstallHelper(new[] { "/u", "/LogFile=", "/LogToConsole=true", Assembly.GetExecutingAssembly().Location });
            break;
    }
    

    【讨论】:

    • 谢谢。此方法仅适用于 LogFile。安装服务时仍在生成 InstallState 文件。有什么方法可以禁用它吗?
    • @JohnSwarley 在最初的问题中没有关于 .InstallState 的内容,我的示例项目没有创建它,因为里面没有安装程序。我想删除 InstallState 的唯一方法是在安装服务后手动删除它。此文件可能对卸载过程很重要:stackoverflow.com/questions/2624848/…
    • @JohnSwarley 你也可以使用/InstallStateDir=[ directoryName ] 参数。尝试在/LogFile= 之前添加/InstallStateDir= 参数。但我担心它会默认使用当前目录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多