Afficher en anglais

Partage via


Console.SetError(TextWriter) Méthode

Définition

Définit la propriété Error avec l'objet TextWriter spécifié.

C#
public static void SetError(System.IO.TextWriter newError);

Paramètres

newError
TextWriter

Flux représentant la nouvelle sortie d'erreur standard.

Exceptions

newError a la valeur null.

L'appelant n'a pas l'autorisation requise.

Exemples

L’exemple suivant montre comment rediriger le flux d’erreurs standard vers un fichier.

C#
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();
   }
}

Remarques

Par défaut, la Error propriété est définie sur le flux de sortie d’erreur standard.

qui StreamWriter encapsule un FileStream peut être utilisé pour envoyer des messages d’erreur à un fichier.

S’applique à

Produit 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, 10
.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

Voir aussi