日志文件 怎么写啊
没写过
给个例子吧
------解决方案--------------------log4net
or
Logging Application Block of Enterprise Library
------解决方案--------------------偶觉得可以自己操作文件,写入捕获的异常
------解决方案--------------------最简单的方法就是写个文件流,在一个TXT文件里写你的程序日志,这是最简单的如果想复杂一些就可以用1楼的方法
------解决方案--------------------using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Test
{
class PublicLogUnit
{
private string GetCurdate()
{
string strDate;
string strYear = System.DateTime.Today.Year.ToString();
string strMonth = System.DateTime.Today.Month.ToString();
if (strMonth.Length == 1)
{
strMonth = "0 " + strMonth;
}
string strDay = System.DateTime.Today.Day.ToString();
if (strDay.Length == 1)
{
strDay = "0 " + strDay;
}
return strDate = strYear + strMonth + strDay;
}
private string GetCurTime()
{
string strTime;
string strHour = System.DateTime.Now.Hour.ToString();
if (strHour.Length == 1)
{
strHour = "0 " + strHour;
}
string strMin = System.DateTime.Now.Minute.ToString();
if (strMin.Length == 1)
{
strMin = "0 " + strMin;
}
string strSec = System.DateTime.Now.Second.ToString();
if (strSec.Length == 1)
{
strSec = "0 " + strSec;
}
return strTime = strHour + ": " + strMin + ": " + strSec;
}
public void WriteToLog(string logMsg)
{
string strDate, strTime;
strDate = GetCurdate();
strTime = GetCurTime();
StreamWriter sw = File.AppendText(System.IO.Directory.GetCurrentDirectory().ToString() + "\\ " + strDate + ".log ");
sw.WriteLine( "[ " + strDate + " " + strTime + "][信息: " + logMsg + "] ");
sw.Close();
}
}
}
------解决方案--------------------log4net 吧.很容易!
------解决方案--------------------就是上面的方法用StreamWriter流来生成一log文件,往里面write内容。。
------解决方案--------------------可以定义一个日志类,根据需要设计日志属性,保存的文件或数据库都可以呀。
也可以用log4net,使用也很简单。
------解决方案--------------------关注