using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
namespace CommonFunction
{
/// <summary>
/// CreateLogFile 的摘要说明。
/// </summary>
public class CreateLogFile : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox txtNewLog;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button WriteToLogbtn;
protected System.Web.UI.WebControls.TextBox txtLogContent;
private void Page_Load(object sender, System.EventArgs e)
{
//读取日志文件内容
txtLogContent.Text = logFileRead();
}
private String logFileRead()
{
//从指定的目录以打开或者创建的形式读取日志文件
FileStream fs = new FileStream(Server.MapPath("upedFile")+"\\logfile.txt", FileMode.OpenOrCreate, FileAccess.Read);
//定义输出字符串
StringBuilder output = new StringBuilder();
//初始化该字符串的长度为0
output.Length = 0;
//为上面创建的文件流创建读取数据流
StreamReader read = new StreamReader(fs);
//设置当前流的起始位置为文件流的起始点
read.BaseStream.Seek(0, SeekOrigin.Begin);
//读取文件
while (read.Peek() > -1)
{
//取文件的一行内容并换行
output.Append(read.ReadLine() + "\n");
}
//关闭释放读数据流
read.Close();
//返回读到的日志文件内容
return output.ToString();
}
public void WriteLogFile(String input)
{
//指定日志文件的目录
string fname = Server.MapPath("upedFile") + "\\logfile.txt";
//定义文件信息对象
FileInfo finfo = new FileInfo(fname);
//判断文件是否存在以及是否大于2K
if ( finfo.Exists && finfo.Length > 2048 )
{
//删除该文件
finfo.Delete();
}
//创建只写文件流
using(FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter w = new StreamWriter(fs);
//设置写数据流的起始位置为文件流的末尾
w.BaseStream.Seek(0, SeekOrigin.End);
//写入“Log Entry : ”
w.Write("\nLog Entry : ");
//写入当前系统时间并换行
w.Write("{0} {1} \r\n", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
//写入日志内容并换行
w.Write(input + "\n");
//写入------------------------------------“并换行
w.Write("------------------------------------\n");
//清空缓冲区内容,并把缓冲区内容写入基础流
w.Flush();
//关闭写数据流
w.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.WriteToLogbtn.Click += new System.EventHandler(this.WriteToLogbtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void WriteToLogbtn_Click(object sender, System.EventArgs e)
{
//写日志文件
WriteLogFile(txtNewLog.Text.Trim());
}
}
}
/*错误信息采集参考
using System;
using System.Collections;
using System.Reflection;
using System.Threading;
using System.Security.Principal;
using System.Text;
using System.IO;
namespace AbnormityManage
{
/// <summary>
/// ErrorLog 的摘要说明。
/// </summary>
public class ErrorLog
{
private static ArrayList alErrorInfo;
public ErrorLog()
{
}
public static void FillErrorInfo(Exception p_exception, ArrayList p_alErrorInfo)
{
alErrorInfo = p_alErrorInfo;
if (alErrorInfo == null)
{
alErrorInfo = new ArrayList();
}
try
{
alErrorInfo.Add("机器名:" + Environment.MachineName);
}
catch
{
alErrorInfo.Add("机器名:未知");
}
try
{
alErrorInfo.Add("时间:"+DateTime.Now.ToString());
}
catch
{
alErrorInfo.Add("时间:未知");
}
try
{
alErrorInfo.Add("程序集名称:"+Assembly.GetExecutingAssembly().FullName);
}
catch
{
alErrorInfo.Add("程序集名称:未知");
}
try
{
alErrorInfo.Add("应用程序域名称:"+AppDomain.CurrentDomain.FriendlyName);
}
catch
{
alErrorInfo.Add("应用程序域名称:未知");
}
try
{
alErrorInfo.Add("线程用户名称:"+Thread.CurrentPrincipal.Identity.Name);
}
catch
{
alErrorInfo.Add("线程用户名称:未知");
}
try
{
alErrorInfo.Add("Windows登陆名称:"+WindowsIdentity.GetCurrent().Name);
}
catch
{
alErrorInfo.Add("Windows登陆名称:未知");
}
}
public static void WriterErrorBlock(Exception exception)
{
StringBuilder builder1 = new StringBuilder();
if (alErrorInfo != null)
{
builder1.AppendFormat("错误信息{0}{1}{0}环境描述:", Environment.NewLine, "*********************************************");
for(int i=0; i<alErrorInfo.Count; i++)
{
builder1.AppendFormat("{0}{1}", Environment.NewLine, alErrorInfo[i].ToString());
}
}
if (exception == null)
{
builder1.AppendFormat("{0}{0}没有错误对象。{0}", Environment.NewLine);
}
else
{
Exception exception1 = exception;
int num1 = 1;
do
{
builder1.AppendFormat("{0}{0}{1}) 错误描述{0}{2}", Environment.NewLine, num1.ToString(), "---------------------------------------------");
builder1.AppendFormat("{0}ErrorType:{1}", Environment.NewLine, exception1.GetType().FullName);
PropertyInfo[] infoArray1 = exception1.GetType().GetProperties();
foreach (PropertyInfo info1 in infoArray1)
{
if ((info1.Name != "InnerException") && (info1.Name != "StackTrace"))
{
if (info1.GetValue(exception1, null) == null)
{
builder1.AppendFormat("{0}{1}:未知", Environment.NewLine, info1.Name);
}
else
{
builder1.AppendFormat("{0}{1}:{2}", Environment.NewLine, info1.Name, info1.GetValue(exception1, null));
}
}
}
if (exception1.StackTrace != null)
{
//builder1.AppendFormat("{0}{0}StackTrace Information{0}{1}", Environment.NewLine, "*********************************************");
builder1.AppendFormat("{0}{1}:{2}", Environment.NewLine,"ErrorUrl", exception1.StackTrace.Trim());
}
exception1 = exception1.InnerException;
num1++;
}
while (exception1 != null);
builder1.AppendFormat("{0}{0}{0}",Environment.NewLine);
}
StreamWriter TextStream = new StreamWriter(@"C:\Log.txt",true);
TextStream.WriteLine(builder1.ToString());
TextStream.Close();
}
}
}*/
发表于 @ 2006年05月31日 12:37:00 | 评论( loading... ) | 举报| 收藏