c#记录日志到文件

发布时间:2020-07-17编辑:admin阅读(908)

public void fileLog(string log)
{
    try
    {
        if (log != "")
        {
            string currentpath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string[] name = currentpath.Split('\\');
            string path = "";
            for (int i = 0; i < name.Length - 1; i++)
            {
                path += name[i] + @"\";
            }
            FileStream fs = new FileStream(path + "log.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            //设定书写的开始位置为文件的末尾
            fs.Position = fs.Length;
            sw.WriteLine(DateTime.Now.ToString() + " 日志:" + log);
            sw.Close();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}


  关键字:c#记录日志文件


鼓掌

0

正能量

0

0

呵呵

0


评论区