gpt4 book ai didi

c# - Console.WriteLine有时会抛出句柄无效异常

转载 作者:行者123 更新时间:2023-12-02 06:14:19 37 4
gpt4 key购买 nike

我有一个 Azure 函数,其中包含以下代码行:

Console.WriteLine("Name: " + list.Count());

此行有时会引发此异常:

The handle is invalid.

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+d__26.MoveNext (Microsoft.Azure.WebJobs.Host, Version=3.0.34.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:352)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+d__18.MoveNext (Microsoft.Azure.WebJobs.Host, Version=3.0.34.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:108)Inner exception System.IO.IOException handled at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw:at System.ConsolePal+WindowsConsoleStream.Write (System.Console, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)at System.IO.StreamWriter.Flush (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)at System.IO.StreamWriter.WriteLine (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)at System.IO.TextWriter+SyncTextWriter.WriteLine (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)at System.Console.WriteLine (System.Console, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)

我们应该忽略这一点还是不要在 Azure 函数中使用控制台类?

最佳答案

当控制台窗口已关闭,或者控制台输出已重定向到文件或其他流时,会发生这种情况。

在 azure 函数中,不应使用 Console.WriteLine。相反,您应该使用 ILogger 来写入日志。您可以编写类似 log.LogInformation("Name: "+ list.Count()); 的日志,这会将输出写入 Azure Functions 运行时日志。您还可以在 Azure 门户中查看日志。

编辑:正如@Fildor在评论中建议的那样,您可以使用log.LogInformation("Name: {count}", list.Count());

关于c# - Console.WriteLine有时会抛出句柄无效异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74937973/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com