dotnet publish报错未写入stderr 如何实现CICD构建错误检测
dotnet 构建错误日志为空问题解决方案
关于dotnet是否支持输出错误到stderr的说明
dotnet命令本身完全支持将错误信息输出到stderr,你遇到error.log为空的核心原因是使用了-flp:logfile=xxx(文件日志参数):
- 该参数会默认接管所有诊断输出(包括错误、警告、常规信息),所有日志会写入你指定的out.log文件,同时错误信息会输出到stdout打印到控制台,不会再输出到stderr
- 因此你用
2>捕获stderr内容的逻辑自然获取不到任何内容
实现构建错误检测的可选方案
方案1:删除-flp参数,用系统重定向拆分日志
不需要使用dotnet内置的文件日志功能,直接用shell重定向拆分正常输出和错误输出即可,命令调整为:dotnet publish tests/dotnet-test-app/dotnet-test-app.csproj -p:PublishSingleFile=true -r linux-musl-x64 -c Release -o .publish/dotnet-test-app > .publish/dotnet-test-app.out.log 2> .publish/dotnet-test-app.error.log调整后你原有判断error.log是否为空的逻辑可以直接复用。
方案2:保留全量日志,通过dotnet退出码判断(更推荐)
dotnet命令执行成功时退出码固定为0,构建/发布失败时退出码为非0值,比判断日志是否为空更准确可靠,不会被警告类信息误触发,命令调整为:dotnet publish tests/dotnet-test-app/dotnet-test-app.csproj -p:PublishSingleFile=true -r linux-musl-x64 -c Release -o .publish/dotnet-test-app -flp:logfile=.publish/dotnet-test-app.out.log if [ $? -ne 0 ]; then # 可选:从全量日志中筛选错误内容单独存储 grep "error :" .publish/dotnet-test-app.out.log > .publish/dotnet-test-app.error.log echo "dotnet-test-app构建失败" exit 1 fi方案3:调整flp参数,用dotnet内置功能拆分错误日志
你可以新增一个文件日志配置,指定仅输出错误到单独的日志文件,不需要shell重定向,命令调整为:dotnet publish tests/dotnet-test-app/dotnet-test-app.csproj -p:PublishSingleFile=true -r linux-musl-x64 -c Release -o .publish/dotnet-test-app -flp:logfile=.publish/dotnet-test-app.out.log -flp1:logfile=.publish/dotnet-test-app.error.log;errorsonly调整后你原有判断error.log是否为空的逻辑可以直接复用。
内容的提问来源于stack exchange,提问作者Stefan
相关产品推荐
相关产品推荐

