How to remove developer's machine paths from log messages?
Asked Answered
G

1

8

When I publish a ASP.NET MVC Core 3.1 application, the logs of exceptions contain the path of the developers machine.

Example of a log:

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()
   ......
   ......

The line number and file location is useful to identify causes of bugs, but the path includes the developer's name FLASTNAME and I'd prefer if that wasn't there.

The closest thing my research lead me to is .pdb files (1, 2), some suggesting to change the debugging information in build settings:

enter image description here

However, I tried every option but it still shows the full paths. I don't have the option to build the project on another machine, so what could I do in this case?

I know I'm probably misunderstanding something here, but even building project in Release configuration doesn't solve my problem.

EDIT:

Here is the result of @Kirk Larkin's suggestion to add <PathMap> code

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

to project's csproj file:

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()
   ......
   ......

So it only affected the path for the HomeController part in the trace. This is similar to what happens if I set Debugging information to None in Advanced Build Settings:

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()
   ......
   ......

except here I don't get any path or the line number for the HomeController part.

Gharry answered 28/4, 2021 at 13:19 Comment(4)
If you're building in CI, there's some settings for Deterministic Builds that will convert those PDB paths to relative to the Source Root path. You may want to look into thatBrowband
Thanks for the suggestion. We are not actually building in CI.Gharry
Have a look at #48240292 and let us know if that works for you.Caesaria
#65936284Kicker
G
15

Thanks to @KirkLarkin, we were able to solve my issue.

The best option for me was to create a single Directory.Build.props file under the solution (the Solution Items folder was generated automatically):

enter image description here

My Directory.Build.props file has the following code:

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

which will map each project's build directory to be just the project's name, resulting in the desired shortened paths:

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()
   ......
   ......

If you need to do this for only one project, you can instead edit the project's .csproj file to contain the <PathMap> code nested inside a <PropertyGroup> tag.

Gharry answered 29/4, 2021 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.