乐趣区

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

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)
            {}}
退出移动版