.Net写txt文件-简单的记录执行日志信息代码

在执行一些批量操作时,想记录一些执行日志信息,越简单方便越好啊。
提供一个常用的简单方法,将信息记录在txt文件里:

public static void log(string content, string path)
        {
            string strFileName = path;
            //判断是否存在
            if (File.Exists(strFileName))
            {
                //存在
                StreamWriter wlog;
                wlog = File.AppendText(strFileName);
                wlog.Write("
{0}", content);
                wlog.Flush();
                wlog.Close();
            }
            else
            {
                //不存在
                FileStream fs1 = new FileStream(strFileName, FileMode.Create, FileAccess.Write);//创建写入文件 
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(content);//开始写入值
                sw.Close();
                fs1.Close();

            }

        }
原文地址:https://www.cnblogs.com/huhangfei/p/5000786.html