3

Using the [CallerFilePath] attribute in C# leads to the inclusion of full local file paths in the compiled CIL binaries, such as C:\Users\MyName\Projects\Project\File.cs.

Is there a way of amending these strings in the compiled binaries? Perhaps as a build step to remove the "C:\Users\MyName\Projects\", but leave the remainder for logging purposes?

Off the top of my head, I can only think of ildasm'ing it, search & replacing, and then ilasming it - but this seems like a rather involved process. I also came across references to the STRINGTABLE resource, but without any implementation details on how to post-process it.

There are a couple of suggested duplicates, including this one, but I don't think this is a duplicate question as this one refers to the [CallerFilePath] attribute specifically, and not to debug-configuration related strings that point to pdb files.

JDR
  • 1,094
  • 1
  • 11
  • 31

1 Answers1

3

You can use the pathMap option for this. It should be available since Roslyn 1.1 and VS 2015 Update 1, though it's currently not documented.

In your specific case, you would add <PathMap>C:\Users\MyName\Projects\Project=Project</PathMap> to your .csproj file. Doing this means [CallerFilePath] will give you Project\File.cs instead of the whole path.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Fantastic - thanks! Good to see that there are Roslyn options for these things. – JDR Jul 25 '17 at 13:11
  • Related: https://stackoverflow.com/questions/48239792/is-it-possible-to-override-or-clear-the-debug-path-in-a-dll-build-with-new-cspro – Jannes Nov 05 '19 at 17:26