首页 > 解决方案 > 使用 AspNetCore Api 设置 NLog 并使用 Docker 进行调试会持续抛出找不到文件错误

问题描述

我在使用 NLog、.NET Core 2.2 AspNet Api 项目和在 Visual Studio 2017 中调试 Docker 容器时遇到了一些问题。

我按照 NLog Github 站点上的设置指南,将代码添加到 Program Main 方法,创建了一个 nlog.config 文件:

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
Debug.Write(logger);
try
{
    logger.Debug("init main");
    CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
    //NLog: catch setup errors
    logger.Error(ex, "Stopped program because of exception");
    throw;
}
finally
{
    // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
    NLog.LogManager.Shutdown();
}

该应用程序运行速度非常慢,配置记录器需要很长时间。但它最终会得到配置文件。

如果我在执行此操作时遵循输出,我会得到一个连续的错误消息列表,如下所示:

Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Private.CoreLib.dll'. Cannot find or open the PDB file.
Loaded '/app/bin/Debug/netcoreapp2.2/SharpGaming.Api.Gateway.Ui.dll'. Symbols loaded.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Runtime.dll'. Module was built without symbols.
Loaded '/root/.nuget/fallbackpackages2/nlog/4.6.5/lib/netstandard2.0/NLog.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/netstandard.dll'. Module was built without symbols.
Loaded '/root/.nuget/fallbackpackages2/nlog.web.aspnetcore/4.8.3/lib/netstandard2.0/NLog.Web.AspNetCore.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Diagnostics.Debug.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.5/Microsoft.AspNetCore.Hosting.Abstractions.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.5/Microsoft.AspNetCore.Hosting.dll'. Cannot find or open the PDB file.
Step into: Stepping over property 'NLog.Config.ConfigurationItemFactory.get_Default'. To step into properties or operators, go to Tools->Options->Debugging and uncheck 'Step over properties and operators (Managed only)'.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Runtime.Extensions.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Collections.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.ComponentModel.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Diagnostics.TraceSource.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Net.Primitives.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Data.Common.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Net.Mail.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Threading.ThreadPool.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.IO.FileSystem.Watcher.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Net.Sockets.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.ComponentModel.TypeConverter.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Threading.Tasks.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Linq.Expressions.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Linq.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Diagnostics.Tools.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Private.Uri.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.IO.FileSystem.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Memory.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Buffers.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Threading.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Runtime.InteropServices.dll'. Module was built without symbols.
Loaded '/root/.nuget/fallbackpackages2/nlog.extensions.logging/1.5.1/lib/netstandard2.0/NLog.Extensions.Logging.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Xml.ReaderWriter.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Private.Xml.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Threading.Thread.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Security.Cryptography.Algorithms.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Text.Encoding.Extensions.dll'. Module was built without symbols.
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Collections.Specialized.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Drawing.Primitives.dll'. Cannot find or open the PDB file.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Threading.Timer.dll'. Module was built without symbols.
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.ComponentModel.Primitives.dll'. Cannot find or open the PDB file.
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Collections.Concurrent.dll'. Cannot find or open the PDB file.
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Loaded '/usr/share/dotnet/shared/Microsoft.AspNetCore.App/2.2.5/Microsoft.AspNetCore.Http.Abstractions.dll'. Cannot find or open the PDB file.
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll

这持续了几个例外

Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Console.dll'. Cannot find or open the PDB file.
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Loaded '/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.5/System.Diagnostics.StackTrace.dll'. Module was built without symbols.
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll

......还有更多。这只是配置记录器。

一旦记录器加载,它将继续执行此操作。

我想也许它是在 Debug 中运行的,但是 DockerFile 已经创建了一个 Release 包。所以我尝试改变它,让它作为 Release 或 Debug 运行,仍然做同样的事情。

部署到 docker 实例的代码不包括我所期望的 PDB 文件。

几乎就像 NLog dll 试图在 Debug 中运行一样,它应该在什么时候在 Release 中运行?

如果我删除 NLog 的设置代码,一切正常。我没有收到任何错误,并且应用程序在 docker 容器中运行平稳快速。

我正在为 Docker 使用 Windows 并创建 Linux Docker。

有人可以帮忙吗?

标签: dockerasp.net-corenlog

解决方案


我想到了...

nlog.config 文件指向 Windows 目录。

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    autoReload="true"
    internalLogLevel="DEBUG"
    internalLogFile="c:\log\apigateway\internal-nlog.txt"
    throwExceptions="true">

删除这些行:

    internalLogLevel="DEBUG"
    internalLogFile="c:\log\apigateway\internal-nlog.txt"

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    autoReload="true"
    throwExceptions="true">

停止抛出的错误。

我通过打开异常设置窗口发现了这一点:Debug > Windows > Exception Settings。转到公共语言运行时异常部分并检查“System.IO.IOException”异常。这样它就会抛出异常。

这给了我一个“无效参数”异常,堆栈跟踪:

at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)

基本上告诉我目录可能有问题?


推荐阅读