Skip to main content

How to Write an Error Message to Standard Error in C#

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A console program has two output channels: standard output for its results and standard error for problems. Console.WriteLine writes to the first, Console.Error.WriteLine to the second, so a warning still reaches the screen when the results go into a file.

Syntax​

Syntax
Console.Error.WriteLine(<message>);
PartWhat it does
Console.ErrorThe standard error stream.
.WriteLine(<message>)Writes the message followed by a new line, as Console.WriteLine does.

The .NET API documentation for Console.Error: "In console applications whose informational output is often redirected to a file, the standard error stream available through the Error property can be used to display information to the console even if output is redirected."

The Example​

The example prints a result with Console.WriteLine and a warning with Console.Error.WriteLine, then sends the output to a file to show which line goes where.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints Report saved. with Console.WriteLine and Warning: 2 rows were skipped. with Console.Error.WriteLine.

main.cs
Console.WriteLine("Report saved.");
Console.Error.WriteLine("Warning: 2 rows were skipped.");

Step 2: Run It, Then Redirect the Output​

Run it, then send standard output to a file with > and look inside the file:

Terminal
dotnet run main.cs
dotnet run main.cs > report.txt
cat report.txt
Output
Report saved.
Warning: 2 rows were skipped.
Warning: 2 rows were skipped.
Report saved.

A zsh terminal where dotnet run main.cs prints both lines, dotnet run main.cs > report.txt prints only the warning, and cat report.txt shows Report saved.

The first run shows both lines. In the second, > sent standard output into report.txt, so only the warning stayed on screen, and cat then shows the report line inside the file. In PowerShell, > also redirects only standard output, and on Windows cat works there as an alias of Get-Content.

Common Mistakes​

MistakeWhat happensDo this instead
Writing warnings with Console.WriteLineThey end up in the redirected file, mixed with the resultsUse Console.Error.WriteLine
Writing results to Console.ErrorThey don't reach the file or the next program in a pipeKeep results on Console.WriteLine
Expecting the error message to stop the programIt only writes; the code carries onReturn a non-zero exit code after it

Troubleshooting​

SymptomLikely causeFix
Error messages are missing from a log fileOnly standard output was redirectedAdd 2> errors.txt to save standard error too
A warning appears inside saved dataIt was written with Console.WriteLineSwitch it to Console.Error.WriteLine

FAQ​

Does Console.Error write in red?​

No. It writes plain text; the difference is the channel, not the color.

How do I save standard error to a file?​

Redirect it with 2>, as in dotnet run main.cs 2> errors.txt. It works the same in zsh, bash and PowerShell.

Conclusion​

Console.WriteLine writes results to standard output and Console.Error.WriteLine writes problems to standard error. Redirect the results and the warnings still show up where you can see them.

Additional Resources​