I've created a fileStream and a streamwriter to write to this. Problem is my file is not showing up with any text. The objects have instantiated correctly and the path and everything is write, just can't see anything writing. Maybe a problem with the streamwriter?
public class Logger {
StreamWriter sw;
FileStream logFileStream;
public enum LogLevel
{
Low,
Medium,
High
};
public Logger(string filePath)
{
//logStream = new StreamWriter(logFilePath, false);
logFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Write);
sw = new StreamWriter(logFileStream);
}
public void LogMessage(string message) {
LogMessage(message, LogLevel.Low, false);
}
public void LogMessage(string message, LogLevel level, bool excludeFromLogFile){
var prefix = string.Empty;
ConsoleColor color = ConsoleColor.White;
switch (level)
{
case LogLevel.Medium:
prefix = "?";
color = ConsoleColor.Yellow;
break;
case LogLevel.High:
prefix = "!";
color = ConsoleColor.Red;
break;
}
if (!excludeFromLogFile)
{
sw.WriteLine("{0} {1} {2}", prefix, DateTime.Now, message);
}
Console.ForegroundColor = color;
Console.WriteLine("{0}", message);
Console.ResetColor();
}
I am instantiating this class and then calling logger.LogMessage("poop", Logger.LogLevel.High, false);
and nothing is showing.
Thanks
The writes are being buffered in memory, try calling logFileStream.Flush();
at the end of each Log function.
You really shouldn't be keeping the file handle open between calls though, if I were you I would open and close it in each function. If you're doing a lot of logging then buffer it yourself in memory and dump the whole thing once it reaches a certain size.