Console.SetError(TextWriter) Console.SetError(TextWriter) Console.SetError(TextWriter) Console.SetError(TextWriter) Method

Définition

Définit la propriété Error avec l'objet TextWriter spécifié.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)

Paramètres

newError
TextWriter TextWriter TextWriter TextWriter

Flux représentant la nouvelle sortie d'erreur standard.A stream that is the new standard error output.

Exceptions

L'appelant n'a pas l'autorisation requise.The caller does not have the required permission.

Exemples

L’exemple suivant montre comment rediriger le flux d’erreurs standard vers un fichier.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();
   }
}
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

Remarques

Par défaut, la Error propriété est définie sur le flux de sortie d’erreur standard.By default, the Error property is set to the standard error output stream.

Un StreamWriter qui encapsule un FileStream peut être utilisé pour envoyer des messages d’erreur à un fichier.A StreamWriter that encapsulates a FileStream can be used to send error messages to a file.

Sécurité

SecurityPermission
pour appeler du code non managé.for calling unmanaged code. Énumération associée:UnmanagedCodeAssociated enumeration: UnmanagedCode

S’applique à

Voir aussi