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();
Potentially similar posts
- Convert escaped Unicode to HTML entities – January 2012
- How to: Get a white screen – June 2011
- My first (useful) Ruby program – June 2011
- Adding your choice of text editor to Flare’s Open With menu – December 2010
- Use the existence of a file on the server to determine Javascript behaviour in the browser – November 2010
September 5th, 2007 at 6:49 am (#)
Why don't you just use log4net from Apache?
June 24th, 2010 at 7:37 am (#)
Thanks a lot. exactly what i was looking for. nice work
November 23rd, 2010 at 10:51 am (#)
you saved my life
November 24th, 2010 at 11:23 pm (#)
ITauthor blog saves a life! That's a first.
:-)
Thanks for the comment.
December 21st, 2010 at 12:13 pm (#)
Thank a lot..
February 24th, 2011 at 8:46 pm (#)
Quick search, the correct result. I feel good with your website. Gonna promote..
April 20th, 2011 at 3:35 pm (#)
Thanks a lot.
May 14th, 2011 at 7:39 pm (#)
Exactly what I'm looking for, big 10x!
June 22nd, 2011 at 7:31 am (#)
simple and working cant ask for more
August 11th, 2011 at 12:59 pm (#)
thanx!
August 21st, 2011 at 3:31 pm (#)
thankx :)
August 22nd, 2011 at 10:17 am (#)
This is really very nice....Exactly want i am looking for thnq so much!!!
September 5th, 2011 at 8:12 am (#)
i will definitely promote.... that's the thing , i was looking for... thanks for sharing ...:)
September 6th, 2011 at 3:54 pm (#)
Thanks for sharing!
October 7th, 2011 at 6:44 am (#)
Thanks for sharing!
November 30th, 2011 at 1:03 am (#)
Exactly what I was looking for.. Thx a lot!
December 2nd, 2011 at 1:47 pm (#)
brilliant, thts wht i was looking for..wil share it....
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(); } }
}
December 7th, 2011 at 9:51 pm (#)
how do u change the location where the file is created?
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?
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.
December 28th, 2011 at 9:08 pm (#)
Just a test: "c:\\testDirectory\\logfile.txt"