当前位置: 移动技术网 > IT编程>开发语言>.net > C# 打印日志

C# 打印日志

2019年05月31日  | 移动技术网IT编程  | 我要评论

大唐房二少,张红亚,兽交母羊

原理其实很简单,就是创建文件夹、创建文件、写入内容

首先判断文件夹、文件是否存在

然后再创建或者追加

不多介绍,直接上代码

public static void buildlogfile(string param)
{
string sfilepath = "e:\\errorlog";
string sfilename =  datetime.now.tostring("yyyymmdd") + ".log";
//文件的绝对路径
sfilename = path.combine(sfilepath, sfilename);
//验证路径是否存在,不存在则创建
if (!directory.exists(sfilepath))
{
directory.createdirectory(sfilepath);
}
filestream fs;
streamwriter sw;
if (file.exists(sfilename))
//验证文件是否存在,有则追加,无则创建
{
fs = new filestream(sfilename, filemode.append, fileaccess.write);
}
else
{
fs = new filestream(sfilename, filemode.create, fileaccess.write);
}
sw = new streamwriter(fs);
//日志内容
sw.writeline(datetime.now.tostring("yyyy-mm-dd hh:mm:ss") + " ----> " + param);
sw.close();
fs.close();
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网