无法写入文件流

时间:2016-04-05 22:58:19

标签: c# .net filestream

我已经创建了一个fileStream和一个写入此文件的编写器。问题是我的文件没有显示任何文字。对象已经正确实例化,路径和一切都是写的,只是看不到任何写作。也许是一个编写者的问题?

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();
        }

我正在实例化这个类,然后调用logger.LogMessage("poop", Logger.LogLevel.High, false);并且没有显示任何内容。

由于

2 个答案:

答案 0 :(得分:2)

写入被缓冲在内存中,尝试在每个Log函数结束时调用logFileStream.Flush();

你真的不应该在调用之间保持文件句柄打开,如果我是你,我会在每个函数中打开并关闭它。如果你正在进行大量的日志记录,那么将它自己缓冲在内存中,并在达到一定大小时将其全部转储。

答案 1 :(得分:1)

这是您的示例的正确版本

  • 在流编写器中使用autoflush = true
  • 在每个请求中打开/关闭流 - 如果正确实现,则不需要autoflush(在处理StreamWriter后将进行刷新)
  • 使用FileMode.Append
    public class Logger
    {
        public enum LogLevel
        {
            Low,
            Medium,
            High
        };

        private readonly string _filePath;

        public Logger(string filePath)
        {
            //logStream = new StreamWriter(logFilePath, false);

            _filePath = filePath;
        }

        public void LogMessage(string message)
        {
            LogMessage(message, LogLevel.Low, false);
        }

        public void LogMessage(string message, LogLevel level, bool excludeFromLogFile)
        {
            using (var fileStream = new FileStream(_filePath, FileMode.Append, FileAccess.Write))
            {
                using (var writer = new StreamWriter(fileStream) {AutoFlush = true})
                {
                    var prefix = string.Empty;
                    var color = ConsoleColor.White;

                    switch (level)
                    {
                        case LogLevel.Medium:
                            prefix = "?";
                            color = ConsoleColor.Yellow;
                            break;
                        case LogLevel.High:
                            prefix = "!";
                            color = ConsoleColor.Red;
                            break;
                    }

                    if (!excludeFromLogFile)
                    {
                        writer.WriteLine("{0} {1} {2}", prefix, DateTime.Now, message);
                    }

                    Console.ForegroundColor = color;
                    Console.WriteLine("{0}", message);
                    Console.ResetColor();
                }
            }
        }
    }