Advertisement
Fhernd

FiltroRegistros.cs

Jul 25th, 2015
21,349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7.  
  8. namespace Receta.CSharp.R0524
  9. {
  10.     public class FiltroRegistros
  11.     {
  12.         public static void Main()
  13.         {
  14.             Console.WriteLine(Environment.NewLine);
  15.            
  16.             // Lista con registros de eventos:
  17.             List<String> registros = new List<String>();
  18.            
  19.             // Adición de entrada de registros:
  20.             registros.Add("Excepción no. 1");
  21.             registros.Add("Excepción no. 2");
  22.             registros.Add("Excepción no. 3");
  23.             registros.Add("Error no. 1");
  24.             registros.Add("Error no. 2");
  25.             registros.Add("Error no. 3");
  26.             registros.Add("Error no. 4");
  27.             registros.Add("Error no. 5");
  28.            
  29.             // Crea archivo de log con todas las entradas:
  30.             File.WriteAllLines("log_completo.log", registros);
  31.            
  32.             // Filtra todos los errores en un nuevo archivo:
  33.             File.WriteAllLines("log_errores.log", registros.Where( e => e.StartsWith("Error")));
  34.            
  35.             // Muestra el contenido del archivo con todos los registros:
  36.             Console.WriteLine ("Registro de eventos completo: ");
  37.             string[] entradas = File.ReadAllLines("log_completo.log");
  38.             foreach (string entrada in entradas)
  39.             {
  40.                 Console.WriteLine (entrada);
  41.             }
  42.            
  43.             // Muestra sólo los registros de error:
  44.             Console.WriteLine ("\nRegistros de eventos (sólo errores): ");
  45.             entradas = File.ReadAllLines("log_errores.log");
  46.             foreach (string entrada in entradas)
  47.             {
  48.                 Console.WriteLine (entrada);
  49.             }
  50.            
  51.             Console.WriteLine(Environment.NewLine);
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement