C#读写日志文本文件
日志为文本文件
每列以制表符隔开 行以换行符隔开
本次示例简单实现如下相关功能:
1.正写日志文本 最新的日志放后面
2.倒写日志文本 最新的日志放前面
3.读日志文本内容显示在Label
4.读日志文本内容到DataTable 及 筛选后显示在GridView
--------------------
(以下操作并没有考虑相关如文件不存在等异常)
//1.正写日志 最新日志放最后面
protected void Button1_Click(object sender, EventArgs e)
{
string strFilePath = Server.MapPath("log/log_200807_1.txt");
System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
sw.WriteLine("'" + DateTime.Now.ToString() + "'\t'zhangsan'\t'Login.aspx'\t'登录A'");
sw.Close();
fs.Close();
}
//2.倒写日志 最新日志放最前面
protected void Button2_Click(object sender, EventArgs e)
{
string strFilePath = Server.MapPath("log/log_200807_1.txt");
string strOldText = File.ReadAllText(strFilePath, System.Text.Encoding.Default);
File.WriteAllText(strFilePath, "'" + DateTime.Now.ToString() + "'\t'zhangsan'\t'Login.aspx'\t'登录B'\r\n", System.Text.Encoding.Default);
File.AppendAllText(strFilePath, strOldText, System.Text.Encoding.Default);
}
//3.读日志文本到Label
protected void Button3_Click(object sender, EventArgs e)
{
string strFilePath = Server.MapPath("log/log_200807_1.txt");
FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
string strLine = sr.ReadLine();
string str = "";
while (strLine != null)
{
str += strLine.ToString() + "<br/>";
strLine = sr.ReadLine();
}
sr.Close();
fs.Close();
this.Label1.Text = str;
}
//4.读日志文本内容到DataTable及筛选后显示在GridView
protected void Button4_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("日志时间");
dt.Columns.Add("操作人员");
dt.Columns.Add("日志页面");
dt.Columns.Add("日志内容");
string strFilePath = Server.MapPath("log/log_200807_1.txt");
FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
string strLine = sr.ReadLine();
while (strLine != null)
{
string[] strArray = new string[4];
strArray = strLine.Split('\t');
DataRow dr = dt.NewRow();
dr[0] = strArray[0];
dr[1] = strArray[1];
dr[2] = strArray[2];
dr[3] = strArray[3];
dt.Rows.Add(dr);
strLine = sr.ReadLine();
}
sr.Close();
fs.Close();
//筛选
DataView dv = dt.DefaultView;
dv.RowFilter = " 日志内容 Like '%A%' and 日志时间 >= '2008-7-8 14:12:50' ";
//this.GridView1.DataSource = dt;
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
}
- 推荐文章
- if语句 与 switch 语句
- 循环
- break 与 continue
- js第一课总结
- 2019.8.12
- for和while循环语句
- 循环总结
- Win11 激活 与 Visual Studio 2022 Keys
- macOS在虚拟机上尝鲜
- vhd应用:VMware Player中实现快照/差分磁盘
- Beginning IOS 7 Development Exploring the IOS SDK
- Beginning IOS 7 Development Exploring the IOS SDK
- 非正规卸载Istio导致pod无法创建
- 致黎明
- 路由器工作原理
- 大学C++程序设计教程期末复习重点
- module.exports与exports的联系与区别
- BFC的特性及使用场景
- 2018 CVTE 前端校招笔试题整理
- JS中new运算符的实现原理
- JS中的原型规则与原型链
- JS中的“==”与强制类型转换
- 协作开发中常用的Git命令小结
- JavaScript变量类型检测总结
- IDEA IntelliJ常用设置以及快捷键(转)
- Spring 发送 Email
- SSM框架的整合思路&功能实现
- 使用Eclipse把java文件打包成jar 含有第三方jar库的jar包
- 基于CDH5.x 下面使用eclipse 操作hive 。使用java通过jdbc连接HIVESERVICE 创建表
- Volley源码学习笔记