Writing to a log file in C#

August 21st, 2007    22 Comments

Here's a chunk of code that will write values to a log file. If the file doesn't exist, it creates it, otherwise it just appends to the existing file. You need to add "using System.IO;" at the top of your code, if it's not already there.

string strLogText = "Some details you want to log.";

// Create a writer and open the file:
StreamWriter log;

if (!File.Exists("logfile.txt"))
{
  log = new StreamWriter("logfile.txt");
}
else
{
  log = File.AppendText("logfile.txt");
}

// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(strLogText);
log.WriteLine();

// Close the stream:
log.Close();

Comments

  1. User Gravatar Dave said:

    September 5th, 2007 at 6:49 am (#)

    Why don't you just use log4net from Apache?

  2. User Gravatar obiora said:

    June 24th, 2010 at 7:37 am (#)

    Thanks a lot. exactly what i was looking for. nice work

  3. User Gravatar andrej_gubin said:

    November 23rd, 2010 at 10:51 am (#)

    you saved my life

  4. User Gravatar itauthor said:

    November 24th, 2010 at 11:23 pm (#)

    ITauthor blog saves a life! That's a first.
    :-)
    Thanks for the comment.

  5. User Gravatar mahesh said:

    December 21st, 2010 at 12:13 pm (#)

    Thank a lot..

  6. User Gravatar Deepak Jose Lopez said:

    February 24th, 2011 at 8:46 pm (#)

    Quick search, the correct result. I feel good with your website. Gonna promote..

  7. User Gravatar Yoav said:

    April 20th, 2011 at 3:35 pm (#)

    Thanks a lot.

  8. User Gravatar Eli said:

    May 14th, 2011 at 7:39 pm (#)

    Exactly what I'm looking for, big 10x!

  9. User Gravatar Or Levy said:

    June 22nd, 2011 at 7:31 am (#)

    simple and working cant ask for more

  10. User Gravatar olish said:

    August 11th, 2011 at 12:59 pm (#)

    thanx!

  11. User Gravatar theGenius! said:

    August 21st, 2011 at 3:31 pm (#)

    thankx :)

  12. User Gravatar Ajay said:

    August 22nd, 2011 at 10:17 am (#)

    This is really very nice....Exactly want i am looking for thnq so much!!!

  13. User Gravatar HariGeek said:

    September 5th, 2011 at 8:12 am (#)

    i will definitely promote.... that's the thing , i was looking for... thanks for sharing ...:)

  14. User Gravatar AngieM said:

    September 6th, 2011 at 3:54 pm (#)

    Thanks for sharing!

  15. User Gravatar Vijay said:

    October 7th, 2011 at 6:44 am (#)

    Thanks for sharing!

  16. User Gravatar .NetStarter said:

    November 30th, 2011 at 1:03 am (#)

    Exactly what I was looking for.. Thx a lot!

  17. User Gravatar tanvir said:

    December 2nd, 2011 at 1:47 pm (#)

    brilliant, thts wht i was looking for..wil share it....

  18. User Gravatar A. Coder said:

    December 6th, 2011 at 11:09 am (#)

    Need to use this in a static context...
    static string strLogText = "Default string to log"; static StreamWriter LogWriter;
    public static void LogToFile(string message)
    {
    try
    {
    if (!File.Exists("LogFile.txt")) { LogWriter = new StreamWriter("LogFile.txt"); }
    else { LogWriter = File.AppendText("LogFile.txt"); }
    LogWriter.WriteLine(string.IsNullOrEmpty(message) ? strLogText : message);
    LogWriter.Flush();
    }
    catch (Exception) { }
    finally { if (LogWriter != null) { LogWriter.Close(); } }
    }

  19. User Gravatar Johan said:

    December 7th, 2011 at 9:51 pm (#)

    how do u change the location where the file is created?

  20. User Gravatar Chris said:

    December 28th, 2011 at 9:03 pm (#)

    For example, change "logfile.txt" to "c:\testDirectory\logfile.txt" in the original code. I believe if you don't use a full path then it defaults to the location of the executable. Thoughts anyone?

  21. User Gravatar Chris said:

    December 28th, 2011 at 9:05 pm (#)

    Hmmm, there are supposed to be 2 backslashes between c: and testDirectory, and 2 between testDirectory and logfile.txt.

  22. User Gravatar Chris said:

    December 28th, 2011 at 9:08 pm (#)

    Just a test: "c:\\testDirectory\\logfile.txt"

Leave a comment