Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress Build Output When Running `dotnet run`

Tags:

c#

.net

build

Is it possible to suppress the build warnings which are output when dotnet run causes a build to occur, e.g. after a code change?

$ dotnet run --verbosity quiet

/../MyProgram.cs(6,21): warning CS8618: Non-nullable property
    'MyProperty' must contain a non-null value when exiting
    constructor. Consider declaring the property as nullable.

<My Program Output>

This is painful, as during development I will pipe my program output into another tool, and the build warning output breaks the parsing of that tool. I don't want to disable any particular warnings; I want to simply omit them from the output of dotnet run.

like image 847
TKF Avatar asked Nov 26 '25 06:11

TKF


2 Answers

$ dotnet run --property WarningLevel=0
  • --property:<NAME>=<VALUE> to pass properties to MSBuild (https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-run)
  • WarningLevel=[0,1,2,3,4] to configure the level of warnings to be output where 4 is the default and 0 disables all warnings (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings)
like image 67
TKF Avatar answered Nov 27 '25 19:11

TKF


I have found that you need multiple switches for this to work more reliably:

dotnet build --nologo -v q --property WarningLevel=0 /clp:ErrorsOnly

where:

  • --nologo suppresses the header
  • -v q sets output verbosity to quiet
  • --property WarningLevel=0 is for MsBuild, and described well in other answers.
  • /clp:ErrorsOnly means ConsoleLoggerParameters

For reasons, I have not fully explored, dotnet run does not always respect these arguments, so I stitch these together with bash/pwsh:

dotnet build --nologo -v q --property WarningLevel=0 /clp:ErrorsOnly && dotnet run --no-build

Disclaimer: disabling warnings is a generally a bad idea

like image 28
Guy Langston Avatar answered Nov 27 '25 18:11

Guy Langston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!