Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自行安装Windows服务会留下安装日志。如何禁用此功能?_C#_.net_Windows_Service_Windows Services - Fatal编程技术网

C# 自行安装Windows服务会留下安装日志。如何禁用此功能?

C# 自行安装Windows服务会留下安装日志。如何禁用此功能?,c#,.net,windows,service,windows-services,C#,.net,Windows,Service,Windows Services,我正在使用以下代码自行安装Windows服务(此代码位于Program.cs中)。当我使用“servicename.exe--install”时,服务安装得很好,但是有些安装日志被留下了。servicename.InstallLog和InstallUtil.InstallLog。有没有办法完全禁用这些文件的日志记录 if (Environment.UserInteractive) { string parameter = string.Concat(arg

我正在使用以下代码自行安装Windows服务(此代码位于Program.cs中)。当我使用“servicename.exe--install”时,服务安装得很好,但是有些安装日志被留下了。servicename.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);
        }

ManagedInstallerClass.InstallHelper
处理Installutil.exe(安装工具)的功能(请参阅)。根据文档(),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;
}

非常感谢。此方法仅适用于日志文件。InstallState文件仍在服务安装时生成。还有什么方法可以禁用它吗?@JohnSwarley最初的问题中没有关于.InstallState的内容,我的示例项目没有创建它,因为里面没有安装程序。我想删除InstallState的唯一方法是在安装服务后手动删除它。此文件对于卸载过程可能很重要:@JohnSwarley您还可以使用
/InstallStateDir=[directoryName]
参数。尝试在
/LogFile=
之前添加
/InstallStateDir=
参数。但我担心默认情况下它只会使用当前目录。