How to Write an Error Message to Standard Error in C#
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
Console.Error.WriteLine(<message>);
| Part | What it does |
|---|---|
Console.Error | The 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
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

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:
dotnet run main.cs
dotnet run main.cs > report.txt
cat report.txt
Report saved.
Warning: 2 rows were skipped.
Warning: 2 rows were skipped.
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
| Mistake | What happens | Do this instead |
|---|---|---|
Writing warnings with Console.WriteLine | They end up in the redirected file, mixed with the results | Use Console.Error.WriteLine |
Writing results to Console.Error | They don't reach the file or the next program in a pipe | Keep results on Console.WriteLine |
| Expecting the error message to stop the program | It only writes; the code carries on | Return a non-zero exit code after it |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Error messages are missing from a log file | Only standard output was redirected | Add 2> errors.txt to save standard error too |
| A warning appears inside saved data | It was written with Console.WriteLine | Switch 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
- Console.Error, on Microsoft Learn.
