简体   繁体   中英

StreamWriter not working c#

im using a lot of streamwrtier in my application, and this one is suppsoed to log errors that happens while the program is running but at the moment it only creates the file withour writing into it :

  if(File.Exists(currentLog))//currentLog = "Path\\Log.txt"
        {
            using (var fileStream = File.Open(currentLog,FileMode.Open))
            {
                StreamWriter ErreurStreamWriter = new StreamWriter(fileStream);
                ErreurStreamWriter.WriteLine("{0}",e.Message);
                ErreurStreamWriter.WriteLine("-------------------------------");
            }
        }
        else
        {
            using (var fileStream = File.Create(currentLog))
            {
                StreamWriter ErreurStreamWriter = new StreamWriter(fileStream);
                ErreurStreamWriter.Write("Liste des erreurs :");
                ErreurStreamWriter.WriteLine("{0}", e.Message);
                ErreurStreamWriter.WriteLine("-------------------------------");
            }
        }

Am I missing something obvious here ? Any help would be great, thanks

写入后可能需要Flush(),或尝试在存在时追加

Try this

        using (var fileStream = File.Open(currentLog, FileMode.Open))
        {
            using (StreamWriter erreurStreamWriter = new StreamWriter(fileStream))
            { 
                erreurStreamWriter.WriteLine("{0}", e.Message);
                erreurStreamWriter.WriteLine("-------------------------------");
            }
        }

Basically what I think happens is that the stream writer never flush into the filestream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM