Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Inconsistent Results When Writing to SD Card


Best Answer bgreer5050, 25 June 2015 - 01:38 AM

The solution is to add 

VolumeInfo.GetVolumes()[0].FlushAll();

after flushing the stream writer.  

using (var filestream = new FileStream(@"SD\Logging\log.txt", FileMode.Append,FileAccess.Write))
{
StreamWriter streamWriter = new StreamWriter(filestream);
streamWriter.WriteLine(strLog + " - " + DateTime.Now.Ticks.ToString());

streamWriter.Flush();
}

VolumeInfo.GetVolumes()[0].FlushAll(); // Force write to sd card
}

Go to the full post


  • Please log in to reply
1 reply to this topic

#1 bgreer5050

bgreer5050

    Member

  • Members
  • PipPip
  • 28 posts

Posted 24 June 2015 - 10:49 PM

I have a static class called Logger with two methods

  1. VerifyFileExists
  2. LogToSD
public static class Logger
    {
        public static void VerifyFileExists()
        {
            try
            {
                if (!File.Exists(@"SD\Logging\log.txt"))
                {
                    if (!Directory.Exists(@"SD\Logging"))
                    {
                        Directory.CreateDirectory(@"SD\Logging");
                    }
                    FileStream _fs = File.Create(@"SD\Logging\log.txt");
                    _fs.Flush();

                }
            }
            catch(System.IO.IOException ex)
            {
                Debug.Print(ex.Message);
            }
        }


        public static void LogToSD(string strLog)
        {
            try
            {
                //FileStream fs = new FileStream(@"SD\Logging\log.txt",FileMode.Append,FileAccess.ReadWrite)
                using (var filestream = new FileStream(@"SD\Logging\log.txt", FileMode.Append,FileAccess.Write))
                {
                    StreamWriter streamWriter = new StreamWriter(filestream);
                    streamWriter.WriteLine(strLog + " - " + DateTime.Now.Ticks.ToString());

                    streamWriter.Flush();
                }
            }
            catch(System.IO.IOException exc)
            {
                Debug.Print(exc.Message);
            }
        }

    }

I call these methods from Main:

 public static void Main()
        {

            Logger.VerifyFileExists();

            Logger.LogToSD("Line One");
            Logger.LogToSD("Line Two");
            Logger.LogToSD("Line Two");
            Logger.LogToSD("Line Two");
            Logger.LogToSD("Line Two");
            Logger.LogToSD("Line Two");
            Logger.LogToSD("Line Two");
        }

I always get a Logger folder and a log.txt file, but less than half the time do I have text in my log.txt file as the result of the Logger.LogToSD calls.  Is there something wrong with my design pattern ?

 

I am running 4.3.2 on a Netduino Plus 2

 



#2 bgreer5050

bgreer5050

    Member

  • Members
  • PipPip
  • 28 posts

Posted 25 June 2015 - 01:38 AM   Best Answer

The solution is to add 

VolumeInfo.GetVolumes()[0].FlushAll();

after flushing the stream writer.  

using (var filestream = new FileStream(@"SD\Logging\log.txt", FileMode.Append,FileAccess.Write))
{
StreamWriter streamWriter = new StreamWriter(filestream);
streamWriter.WriteLine(strLog + " - " + DateTime.Now.Ticks.ToString());

streamWriter.Flush();
}

VolumeInfo.GetVolumes()[0].FlushAll(); // Force write to sd card
}






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.