Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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# 启动服务未返回有关故障的足够信息_C#_Powershell_Windows Services_.net Core 3.1 - Fatal编程技术网

C# 启动服务未返回有关故障的足够信息

C# 启动服务未返回有关故障的足够信息,c#,powershell,windows-services,.net-core-3.1,C#,Powershell,Windows Services,.net Core 3.1,问题是,当我从OctopusDeploy下启动Windows服务时,如果服务无法启动,则错误消息的描述不够。。我正在使用.net core 3.1和Microsoft.Extensions.Hosting.WindowsServices包创建windows服务。很容易在本地重现该问题: 启动服务MySvc 以下错误显示在PowerShell 启动服务:由于以下错误,无法启动服务“MySvc(MySvc)”:无法在计算机“”上启动服务MySvc。第1行字符:1 启动服务MySvc + Catego

问题是,当我从
OctopusDeploy
下启动Windows服务时,如果服务无法启动,则错误消息的描述不够。。我正在使用
.net core 3.1
Microsoft.Extensions.Hosting.WindowsServices
包创建windows服务。很容易在本地重现该问题:

启动服务MySvc

以下错误显示在
PowerShell

启动服务:由于以下错误,无法启动服务“MySvc(MySvc)”:无法在计算机“”上启动服务MySvc。第1行字符:1

启动服务MySvc

+ CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException

+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
如您所见,它缺少有关错误消息的任何信息。

构建器如下所示:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseWindowsService()
            .ConfigureServices((hostContext, services) =>
            {
                services.DoCustomLogic();
                services.AddHostedService<Mysvc>();
            })
            .ConfigureLogging(loggingBuilder =>
            {
                loggingBuilder
                    .AddConsole(options => { options.IncludeScopes = true; })
                    .AddEventLog();
            });
此外,启动失败后-在
事件查看器中有2条新记录:
.Net运行时
输出:

Application: MySvc.exe
CoreCLR Version: 4.700.19.60701
.NET Core Version: 3.1.1
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Exception: my custom exception
...
异常信息:系统。异常:我的自定义异常

所以基本上,我就是那个真正抛出异常的人,我知道错误是什么。我的问题是-这是否可能将有关此类异常的一些更有意义的信息输出到
powershell
控制台

try {
    Start-Service MySvc -ErrorAction Stop
} catch {
    # Examine and play with one of the following objects
    Write-Host "$($error[0].Exception)"
    Write-Host "$($_.exception)"
}
对于使用
八达通部署的用户来说
——日志中没有显示实际的错误消息是违反直觉的,因此他们必须登录到服务器并检查事件查看器日志


我想一个可能的解决方案是创建一个生成后脚本来检查事件。。但是如果可能的话,我宁愿用C来解决它。

不幸的是,我无法为您测试它,但是添加
-ErrorAction Stop
并捕获异常怎么样

try {
    Start-Service MySvc -ErrorAction Stop
} catch {
    # Examine and play with one of the following objects
    Write-Host "$($error[0].Exception)"
    Write-Host "$($_.exception)"
}

不幸的是,我无法为您测试它,但是添加
-ErrorAction Stop
并捕获异常如何

try {
    Start-Service MySvc -ErrorAction Stop
} catch {
    # Examine and play with one of the following objects
    Write-Host "$($error[0].Exception)"
    Write-Host "$($_.exception)"
}

我最终创建了
octopusdeploy
脚本,用于打印Windows事件中的最后3个错误

$EntryType = 'Error'
$LogName = 'Application'
$Source = '.NET Runtime'
$EventCount = 3

Write-Host ' '
Write-Host "Searching for the last $EventCount events of source $Source.."
$evts = Get-EventLog -LogName $LogName -Newest $EventCount -EntryType $EntryType -Source $Source -ErrorAction SilentlyContinue # | Format-List -Property *
if($evts){
    Write-Host ' '
    $i = 1;
    $evts | ForEach-Object -Process {
        Write-Host "`tPrinting event $i"

        $obj = $_#$evts | Select-Object -Property TimeGenerated, Message
        $time = $obj."TimeGenerated"
        $message = $obj."Message" # or $obj | Select -ExpandProperty "SomeProp"

        Write-Host "`tTime: $time"
        Write-Host "`tMessage: $message"
        Write-Host ' '
        $i++;
    }

} else{
    Write-Host "No events found with the name $Source"
}

Write-Host ' '
Exit 0

我最终创建了
octopusdeploy
脚本,用于打印Windows事件中的最后3个错误

$EntryType = 'Error'
$LogName = 'Application'
$Source = '.NET Runtime'
$EventCount = 3

Write-Host ' '
Write-Host "Searching for the last $EventCount events of source $Source.."
$evts = Get-EventLog -LogName $LogName -Newest $EventCount -EntryType $EntryType -Source $Source -ErrorAction SilentlyContinue # | Format-List -Property *
if($evts){
    Write-Host ' '
    $i = 1;
    $evts | ForEach-Object -Process {
        Write-Host "`tPrinting event $i"

        $obj = $_#$evts | Select-Object -Property TimeGenerated, Message
        $time = $obj."TimeGenerated"
        $message = $obj."Message" # or $obj | Select -ExpandProperty "SomeProp"

        Write-Host "`tTime: $time"
        Write-Host "`tMessage: $message"
        Write-Host ' '
        $i++;
    }

} else{
    Write-Host "No events found with the name $Source"
}

Write-Host ' '
Exit 0

我不是专家,但是
启动服务
只会检查服务是否成功启动,这听起来并不是不合理的。服务背后可以有任何类型的进程,不仅仅是
.net
进程,所以我不明白为什么会有一种机制允许抛出
.net
异常,将各种API备份到启动服务的实体或进程。事件日志确实是获取此信息的正确位置。我不是专家,但
启动服务
只会检查服务是否成功启动听起来并不合理。服务背后可以有任何类型的进程,不仅仅是
.net
进程,所以我不明白为什么会有一种机制允许抛出
.net
异常,将各种API备份到启动服务的实体或进程。事件日志确实是此信息的正确位置。不幸的是,显示的消息与前面描述的相同..不幸的是,显示的消息与前面描述的相同。。