读写文件之日志文件
来源:互联网 发布:autodesk maya mac版 编辑:程序博客网 时间:2024/06/15 23:11
我们在写软件的时候,经常要记录一些登陆信息、删除信息之类,便于日后查询。我简单写了一个针对日志文件的类,可以通过此类可以自定义日志文件名称,当日志达到规定大小时,自动备份,路径可以自行定义具体如下:
命名空间:
using System;
using System.Web;
using System.IO;
using System.Text;
具体实现:
public class LogFile
{
protected string LogfileName; // 文件名称
protected string LogPath = "../upedFile"; // 文件路径
protected int LogMaxContent = 2048; // 文件大小
protected string InputContent; // 具体内容
public LogFile()
{
}
public LogFile(string StrLogfileName,string StrInputContent,string StrLogPath)
{
LogfileName = StrLogfileName;
InputContent = StrInputContent;
LogPath = StrLogPath;
}
public void LogWrite()
{
// string PathName = System.Web.HttpContext.Current.Server.MapPath(LogPath) + LogfileName;
string PathName = System.Web.HttpContext.Current.Server.MapPath(LogPath) + "//" + LogfileName;
FileInfo Finfo = new FileInfo(PathName);
string PathNameMove = PathName.Substring(0,PathName.LastIndexOf("//"))+"//" + DateTime.Now.ToString("yyyyMMddhhmm") + LogfileName;
if( Finfo.Exists && Finfo.Length > LogMaxContent ) // 如果超出,重名名
{
Finfo.CopyTo(PathNameMove);
Finfo.Delete();
}
try
{
using(FileStream Fs = Finfo.OpenWrite())
{
StreamWriter Sw = new StreamWriter(Fs);
Sw.BaseStream.Seek(0, SeekOrigin.End); //设置写数据流的起始位置为文件流的末尾
StringBuilder StrInput = new StringBuilder(); // 记录写入的内容
StrInput.Append("/r/n Log Entry : ");
StrInput.Append(DateTime.Now.ToString());
StrInput.Append("/r/n");
StrInput.Append(InputContent + "/r/n");
StrInput.Append("------------------------------------/n");
Sw.Write(StrInput);
Sw.Flush();
Sw.Close();
}
}
catch
{
System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('日志创建失败')</script>");
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=628032
- 读写文件之日志文件
- 读写文件之日志文件
- 读写文件之日志文件
- 读写文件之日志文件
- 文件上传之文件读写
- Unity3D之文件读写文件
- C#之文件读写
- python之读写文件
- qt 之读写文件
- iOS之文件读写
- Android之文件读写
- Unity3D之文件读写
- 数据处理之文件读写
- 数据处理之文件读写
- Qt之读写文件
- Python之文件读写
- Python之文件读写
- Python之读写文件
- LzmTW.uSystem.uWindows.Win32API+MouseKeyboardHook
- 不用API实现截屏/截取当前窗口内容(“抓图”)
- Java学习之路:不走弯路,就是捷径
- 中文双引号的问题
- Vertex Shader 结构
- 读写文件之日志文件
- LzmTW.uSystem.uWindows.Win32API+KeyStatusWather
- 面试问题大全
- C#播放rm文件
- 获得局域网中计算机的列表(包括计算机名,IP和MAC)的方法
- 2006年各种主流CMS系统的比较和汇总
- 追MM与设计模式
- 新手问题
- 发送传真的c#方法