Console.SetError(TextWriter) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 Error 属性设置为指定的 TextWriter 对象。
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
一个流,它是新的标准错误输出。
例外
newError
为 null
。
调用方没有所要求的权限。
示例
以下示例演示如何将标准错误流重定向到文件。
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
注解
默认情况下, Error 属性设置为标准错误输出流。
StreamWriter封装 的 可用于将 FileStream 错误消息发送到文件。