Console.SetError(TextWriter) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Sets the Error property to the specified TextWriter object.
public:
static void SetError(System::IO::TextWriter ^ newError);
public static void SetError (System.IO.TextWriter newError);
static member SetError : System.IO.TextWriter -> unit
Public Shared Sub SetError (newError As TextWriter)
- newError
- TextWriter
A stream that is the new standard error output.
newError
is null
.
The caller does not have the required permission.
The following example shows how to redirect the standard error stream to a file.
using namespace System;
using namespace System::IO;
using namespace System::Reflection;
ref class RedirectStdErr;
void main()
{
// Define file to receive error stream.
DateTime appStart = DateTime::Now;
String^ fn = "c:\\temp\\errlog" + appStart.ToString("yyyyMMddHHmm") + ".log";
TextWriter^ errStream = gcnew StreamWriter(fn);
String^ appName = Assembly::GetExecutingAssembly()->Location;
appName = appName->Substring(appName->LastIndexOf('\\') + 1);
// Redirect standard error stream to file.
Console::SetError(errStream);
// Write file header.
Console::Error->WriteLine("Error Log for Application {0}", appName);
Console::Error->WriteLine();
Console::Error->WriteLine("Application started at {0}.", appStart);
Console::Error->WriteLine();
//
// Application code along with error output
//
// Close redirected error stream.
Console::Error->Close();
}
using System;
using System.IO;
using System.Reflection;
public class RedirectStdErr
{
public static void Main()
{
// Define file to receive error stream.
DateTime appStart = DateTime.Now;
string fn = @"c:\temp\errlog" + appStart.ToString("yyyyMMddHHmm") + ".log";
TextWriter errStream = new StreamWriter(fn);
string appName = typeof(RedirectStdErr).Assembly.Location;
appName = appName.Substring(appName.LastIndexOf('\\') + 1);
// Redirect standard error stream to file.
Console.SetError(errStream);
// Write file header.
Console.Error.WriteLine("Error Log for Application {0}", appName);
Console.Error.WriteLine();
Console.Error.WriteLine("Application started at {0}.", appStart);
Console.Error.WriteLine();
//
// Application code along with error output
//
// Close redirected error stream.
Console.Error.Close();
}
}
open System
open System.IO
open System.Reflection
[<EntryPoint>]
let main _ =
// Define file to receive error stream.
let appStart = DateTime.Now
let fn = @"C:\temp\errlog" + appStart.ToString "yyyyMMddHHmm" + ".log"
use fs = new FileStream(fn, FileMode.OpenOrCreate)
let errStream = new StreamWriter(fs)
let appName =
let appName = Assembly.GetExecutingAssembly().Location
appName.Substring(appName.LastIndexOf('\\') + 1)
// Redirect standard error stream to file.
Console.SetError errStream
// Write file header.
Console.Error.WriteLine $"Error Log for Application {appName}"
Console.Error.WriteLine()
Console.Error.WriteLine $"Application started at {appStart}."
Console.Error.WriteLine()
//
// Application code along with error output
//
// Close redirected error stream.
Console.Error.Close()
0
Imports System.IO
Imports System.Reflection
Module RedirectStdErr
Public Sub Main()
' Define file to receive error stream.
Dim appStart As Date = Date.Now
Dim fn As String = "c:\temp\errlog" & appStart.ToString("yyyyMMddHHmm") & ".log"
Dim errStream As New StreamWriter(fn)
Dim appName As String = GetType(RedirectStdErr).Assembly.Location
appName = Mid(appName, InStrRev(appName, "\") + 1)
' Redirect standard error stream to file.
Console.SetError(errStream)
' Write file header.
Console.Error.WriteLine("Error Log for Application {0}", appName)
Console.Error.WriteLine()
Console.Error.WriteLine("Application started at {0}.", appStart)
Console.Error.WriteLine()
'
' Application code along with error output
'
' Close redirected error stream.
Console.Error.Close()
End Sub
End Module
By default, the Error property is set to the standard error output stream.
A StreamWriter that encapsulates a FileStream can be used to send error messages to a file.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.6, 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: