当前位置:首页 > 计算机 > 正文内容

C#读写日志文本文件

DYX5年前 (2020-08-18)计算机803

日志为文本文件
每列以制表符隔开 行以换行符隔开

本次示例简单实现如下相关功能:
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();
}

//取得当前所应操作的日志文件的路径 
private string GetLogFilePath() 

string strFilePath = ""; 
string strYearMonth = DateTime.Now.ToString("yyyyMM"); 
string strLogDirPath = Server.MapPath("log"); 
//判断当前月份是否已有日志文件 
string[] strFilesArray = Directory.GetFiles(strLogDirPath, "log_" + strYearMonth + "_*.txt"); 
if (strFilesArray.Length == 0) 

strFilePath = Server.MapPath("log/log_" + strYearMonth + "_1.txt"); 
//之前没有本年月的日志文件 需要新建 
using (File.Create(strFilePath)) 




else 

int intOrderID = 1; 
for (int i = 0; i < strFilesArray.Length; i++) 

string strA = strFilesArray[i].Trim(); 
strA = strA.Substring(strA.LastIndexOf("_")+1); 
strA = strA.Replace(".txt", ""); 
int intA = Convert.ToInt32(strA); 
if (intA > intOrderID) 
intOrderID = intA; 


strFilePath = Server.MapPath("log/log_" + strYearMonth + "_" + intOrderID.ToString() + ".txt"); 
this.Label1.Text = strFilePath; 
//之前有 需要判断最后一个是否超容 
FileInfo fileInfo = new FileInfo(strFilePath); 
if (fileInfo.Length >= 1024) 

//超容了 新建之 
int intCount = intOrderID + 1; 
strFilePath = Server.MapPath("log/log_" + strYearMonth + "_" + intCount.ToString() + ".txt"); 
using (File.Create(strFilePath)) 





return strFilePath ; 
}

讀寫ini文件 
[DllImport("kernel32")] 
private static extern long WritePrivateProfileString(string section, 
string key,string val,string filePath); 
[DllImport("kernel32")] 
private static extern int GetPrivateProfileString(string section, 
string key,string def, StringBuilder retVal,int size,string filePath); 

public void IniWriteValue(string Section,string Key,string Value,string filePath) 

WritePrivateProfileString(Section,Key,Value,filePath); 
public string IniReadValue(string Section,string Key,string filePath) 

StringBuilder temp = new StringBuilder(255); 
int i = GetPrivateProfileString(Section,Key,"",temp, 
255, filePath); 
return temp.ToString(); 



}  

获取指定文件夹的大小 
public long countsize( System.IO.DirectoryInfo dir) 

long size=0; 
FileInfo[] files=dir.GetFiles(); 
foreach(System.IO.FileInfo info in files) 

size+=info.Length; 

DirectoryInfo[] dirs=dir.GetDirectories(); 
foreach(DirectoryInfo dirinfo in dirs) 

size+=countsize(dirinfo); 

return size; 
}   

读取文件: 
ResourceManager _textResManager = new ResourceManager("testproject.MultiLanguage_Eng", Assembly.GetExecutingAssembly()); 
string resString = _textResManager.GetString("keyname");


“C#读写日志文本文件” 的相关文章

QPS是什么意思?一般的服务器qps多少?

QPS是什么意思?一般的服务器qps多少?

QPSQPS即每秒查询率,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准。 每秒查询率因特网上,经常用每秒查询率来衡量域名系统服务器的机器的性能,其即为QPS。对应fetches/sec,即每秒的响应请求数,也即是最大吞吐能力。计算关系:QPS = 并发量 / 平均响应时间并...

Win10企业版和LTSB\LTSC分支有什么不同

Win10企业版和LTSB\LTSC分支有什么不同

Win10企业版相对于家庭版、教育版、专业版的用户人群不同,面向企业用户。但是Win10又出现了LTSB版和N版。这个LTSB版和N版是什么?两者区别在哪?微软弱的服务器操作系统Server2019也有长期服务版本LTSC版,提供10年技术支持。对于企业来说,有技术支持最好。Win10 LTSB 2...

Linux常用命令思维导图

Linux常用命令思维导图

wget http://192.168.2.102/a.txt 到指定域名下下载指定文件查看命令帮助(b向上移动,空格键向下,q退出) man 命令名 (查看命令详细说明) 命令名 –help (查看命令的常用选项)关机和重启 关机:shutdown -h now (n...

隐藏用户没有权限的数据库,让他只能看到自己拥有权限的数据库

隐藏用户没有权限的数据库,让他只能看到自己拥有权限的数据库

隐藏用户没有权限的数据库,让他只能看到自己拥有权限的数据库这个很多人都问的问题,可能很多都有没有得到答案希望可以帮到你:隐藏用户没有权限的数据库,让他只能看到自己拥有权限的数据库这个很多人都问的问题,可能很多都有没有得到答案首先你阅读以下ms关于VIEW ANY DATABASE...

centos 安装xfce轻量桌面环境

centos 安装xfce轻量桌面环境

1. 安装额外yum源yum install epel-release2. 安装依赖组件yum upgradeyum -y groupinstall "X Window System"3. 安装xfce桌面环境yum -y groupinstall X11yum --enable...

AArch64、x86是什么

AArch64、x86是什么

AMD,中文名(超威)超微半导体,是除了英特尔以外最大的x86架构微处理供应商,也是除了英伟达以外仅有的独立图形处理供应商。x86泛指一系列由英特尔公司开发的处理器的架构,最早为1978年面世的“Intel 8086”CPU。早期的处理器均是以此格式来命名,如Intel 8086,80186,802...