关于c#:71c-创建logtxt文件并写日志

40次阅读

共计 563 个字符,预计需要花费 2 分钟才能阅读完成。

private void WriteLog(string content)
        {
            try
            {string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log/log_win.txt");
                content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + content;

                bool append = true;

                if (System.IO.File.Exists(filePath) == true)
                {var fileinfo = new System.IO.FileInfo(filePath);
                    if (fileinfo.Length >= 1024 * 1024 * 10) // 如果大于等于 1M(1024B*1024KB),则重写日志。{append = false;}
                }

                var logFileStream = new System.IO.StreamWriter(filePath, append, System.Text.Encoding.UTF8);
                logFileStream.WriteLine(content);
                logFileStream.Flush();
                logFileStream.Close();}
            catch(Exception e)
            {}}

正文完
 0