How to create a custom error log in C#

How to create a custom error log in C#

This is the easiest way to track your errors in your system. Save the details in a text file, here’s how to do it.

Add a public class in your project so that it would be globally used.

public void ErrLogs(string log, string path)
{
string sLogFormat = DateTime.Now.ToShortDateString().ToString() + ” ” + DateTime.Now.ToLongTimeString().ToString() + ” ==> “;
string spath = path + DateTime.Now.ToShortDateString().ToString() + “.txt”;

FileStream fs = new FileStream(spath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
string value = sLogFormat + log;
m_streamWriter.WriteLine(value);
m_streamWriter.Flush();
m_streamWriter.Close();
}

This is what it looks like in your text file.

Leave a Comment