C# 写入文本日志(winform使用)

C# 1704次浏览 本站
using System;
using System.IO;
using System.Threading;

namespace RC.Software.ClientTool
{
    public class LogHelper
    {
        /// <summary>
        ///     写入文本日志
        /// </summary>
        /// <param name="msg"></param>
        public static void LogToFile(string msg)
        {
            var dir = Thread.GetDomain().BaseDirectory + "Log";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var filePath = Path.Combine(dir, DateTime.Now.ToString("yyyy-MM-dd") + "_log.txt");
            using (var stream = new FileStream(filePath, FileMode.Append))
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + msg);
                }
            }
        }
    }
}
            

发表评论

电子邮件地址不会被公开。 必填项已用*标注