如何从日志消息中删除开发人员的机器路径?

时间:2021-04-28 13:19:01

标签: c# asp.net-mvc asp.net-core build pdb-files

当我发布 ASP.NET MVC Core 3.1 应用程序时,异常日志包含开发人员机器的路径。

日志示例:

2021-04-27 16:36:47.1954 | |18f76390-4123opr9fjnfe832. | ERROR | Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware | An unhandled exception has occurred while executing the request. | System.Exception: TEST
   at ClassLibrary.Repositories.HomeRepo.GetSummary() in C:\Users\FLASTNAME\TFS\dotnet\APPNAME\.......:line 68
   at ClassLibrary.Services.HomeService.GetStatuses() in C:\Users\FLASTNAME\TFS\dotnet\APPNAME\.......:line 38
   at Project.Controllers.HomeController.GetSummary() in C:\Users\FLASTNAME\TFS\dotnet\APPNAME\.......\HomeController.cs:line 39
   at lambda_method(Closure , Object )
   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
   ......
   ......

行号和文件位置对于确定错误的原因很有用,但路径包含开发人员的姓名 FLASTNAME,如果没有,我更愿意这样做。

我的研究使我最接近的是 .pdb 文件(12),有些人建议更改构建设置中的调试信息:

enter image description here

然而,我尝试了所有选项,但它仍然显示完整路径。我无法选择在另一台机器上构建项目,那么在这种情况下我该怎么办?

我知道我可能在这里误解了一些东西,但即使在 Release 配置中构建项目也不能解决我的问题。

编辑:

这是@Kirk Larkin 建议添加 <PathMap> 代码的结果

<PropertyGroup>
  <PathMap>$(MSBuildProjectDirectory)=ANOTHERPATH</PathMap>
</PropertyGroup>

到项目的 csproj 文件:

2021-04-27 16:36:47.1954 | |18f76390-4123opr9fjnfe832. | ERROR | Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware | An unhandled exception has occurred while executing the request. | System.Exception: TEST
   at ClassLibrary.Repositories.HomeRepo.GetSummary() in C:\Users\FLASTNAME\TFS\dotnet\APPNAME\.......:line 68
   at ClassLibrary.Services.HomeService.GetStatuses() in C:\Users\FLASTNAME\TFS\dotnet\APPNAME\.......:line 38
   at Project.Controllers.HomeController.GetSummary() in ANOTHERPATH\Controllers\HomeController.cs:line 39
   at lambda_method(Closure , Object )
   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
   ......
   ......

所以它只影响跟踪中 HomeController 部分的路径。这与在高级构建设置中将调试信息设置为None时发生的情况类似:

2021-04-27 16:36:47.1954 | |18f76390-4123opr9fjnfe832. | ERROR | Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware | An unhandled exception has occurred while executing the request. | System.Exception: TEST
   at ClassLibrary.Repositories.HomeRepo.GetSummary() in C:\Users\FLASTNAME\TFS\dotnet\APPNAME\.......:line 68
   at ClassLibrary.Services.HomeService.GetStatuses() in C:\Users\FLASTNAME\TFS\dotnet\APPNAME\.......:line 38
   at Project.Controllers.HomeController.GetSummary()
   at lambda_method(Closure , Object )
   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
   ......
   ......

除了这里我没有得到 HomeController 部分的任何路径或行号。

1 个答案:

答案 0 :(得分:1)

感谢@KirkLarkin,我们解决了我的问题。

对我来说最好的选择是在解决方案下创建一个 Directory.Build.props 文件(Solution Items 文件夹是自动生成的):

enter image description here

我的 Directory.Build.props 文件有以下代码:

<Project>
    <PropertyGroup>
        <PathMap>$(MSBuildProjectDirectory)=$(MSBuildProjectName)</PathMap>
    </PropertyGroup>
</Project>

这会将每个项目的构建目录映射为项目名称,从而生成所需的缩短路径:

2021-04-27 16:36:47.1954 | |18f76390-4123opr9fjnfe832. | ERROR | Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware | An unhandled exception has occurred while executing the request. | System.Exception: TEST
   at ClassLibrary.Repositories.HomeRepo.GetSummary() in ClassLibrary\.......:line 68
   at ClassLibrary.Services.HomeService.GetStatuses() in ClassLibrary\.......:line 38
   at Project.Controllers.HomeController.GetSummary() in Project\.......\HomeController.cs:line 39
   at lambda_method(Closure , Object )
   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
   ......
   ......

如果您只需要为一个项目执行此操作,您可以改为编辑项目的 .csproj 文件,以包含嵌套在 <PathMap> 标记中的 <PropertyGroup> 代码。