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.

4 thoughts on “How to create a custom error log in C#”

  1. Hey There. I found your blog using msn. This is a really well written article.

    I’ll make sure to bookmark it and return to read more of
    your useful information. Thanks for the post. I will
    certainly comeback.

    Reply
  2. Thank you a bunch for sharing this with all of
    us you really recgnize what you are speaking about! Bookmarked.
    Please additionally seek advice from my web site =).
    We could have a link exchange arrangement betyween us

    Reply

Leave a Comment