Error Log with StreamWriter - CSharp System.IO

CSharp examples for System.IO:StreamWriter

Description

Error Log with StreamWriter

Demo Code


using System.Text;
using System.Linq;
using System.IO;//  w  w w  .  ja  v  a 2 s  .co  m
using System.Collections.Generic;
using System;

public class Main{
        public static void ErrorLog(string error)
        {
            try
            {
                //Pass the filepath and filename to the StreamWriter Constructor
                StreamWriter sw = new StreamWriter(Path.GetFullPath("./ProgErrorLogs.log"), true, Encoding.GetEncoding(932));

                //Write a line of text
                sw.WriteLine(string.Format("{0} : {1}", DateTime.Now, error));

                //Close the file
                sw.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
}

Related Tutorials