Console.SetError(TextWriter) 메서드

정의

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 사용하여 오류 메시지를 파일에 보낼 수 있습니다.

적용 대상

추가 정보