I have a static class called Logger with two methods
- VerifyFileExists
- 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