C# 保存程序运行日志

C# 保存程序运行日志

猿掌柜
2021-09-07 / 1 评论 / 27 阅读 / 正在检测是否收录...

直接调用即可,默认程序

需引用如下两条

using System.IO;
using System.Text;
public static void WriteLogs(int macid,  string v)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string content = macid + v;
            if (!string.IsNullOrEmpty(path))
            {
                path = AppDomain.CurrentDomain.BaseDirectory +"NetLogs";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd ") + "_" + DateTime.Now.ToString("HH") + ".txt";
                if (!File.Exists(path))
                {
                    FileStream fs = File.Create(path);
                    fs.Close();
                }
                if (File.Exists(path))
                {
                    StreamWriter sw = new StreamWriter(path, true, Encoding.Default);
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "-->" + content);
                    //  sw.WriteLine("----------------------------------------");
                    sw.Close();
                }
            }
        }
3

评论 (1)

取消
  1. 头像
    过客
    MacOS · QQ Browser

    学习了

    回复