Search code examples
c#msbuilddotnet-clicompiler-optionsdotnet-sdk

C# compiler option ErrorLog how to use project name or assmeblyname as log file name in msbuild or dotnet cli


There is option in c# compiler for getting code analysis log using errorlog option, refer https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings

if we give just string it works but $(ProjectName) or $(AssemblyName) does not work it treat it as string instead project property

msbuild mysolution.sln /property:ErrorLog=$(AssemblyName).sarif

above command generate log file as $(AssemblyName).sarif instead MyProject.sarif

How to correctly use errolog switch to get project name prepended to log file name string.


Solution

  • The command line /property switch doesn't evaluate the values for property references. A value like $(AssemblyName).sarif will be taken as a literal. The property reference will not be expanded.

    Instead create a Directory.Build.props file in the solution folder (or a folder that contains all the project folders) with the following content:

    <Project>
     <PropertyGroup>
       <ErrorLog>$(AssemblyName).sarif</ErrorLog>
     </PropertyGroup>
    </Project>