Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to save logs in .txt format and I am able to do this. But I am not able to save more than one logs error. I am trying as like this....

C#
try
            {
                if (!string.IsNullOrEmpty(message))
                {
                    using (FileStream file = new FileStream("ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        StreamWriter streamWriter = new StreamWriter(file);
                        streamWriter.WriteLine((((System.DateTime.Now + " - ") + fileName + " - ") + methodName + " - ") + message);
                        streamWriter.Close();
                    }
                }
            }
            catch
            {
            }
Posted

Use my article here instead : Mini Drop-in Replacement for log4net[^]
 
Share this answer
 
use FileMode.Append instead of FileMode.OpenOrCreate
 
Share this answer
 
C#
public void WriteLogFile(string fileName, string methodName, string message)
        {
            try
            {
                if (!string.IsNullOrEmpty(message))
                {
                    //using (FileStream file = new FileStream("ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.Write))
                    //{
                        //StreamWriter streamWriter = new StreamWriter(file);
                        StreamWriter streamWriter = File.AppendText("ErrorLog.txt");

                        streamWriter.WriteLine((((System.DateTime.Now + " - ") + fileName + " - ") + methodName + " - ") + message);
                        streamWriter.Close();
                    //}
                }
            }
            catch
            {
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900