以前的日志类:
public static void LogWriteText(string TraceMsg)
{
try
{
int TempFileSize = System.Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["logFileLength"]);
FileSize = (TempFileSize == 0) ? 1024 * 1024 * 1024 : TempFileSize;
}
catch
{
FileSize = 1024 * 1024 * 1024;
}
if (ConfigurationManager.AppSettings["writeLog"].ToString() != "0")
{
try
{
//string filePath = Path.Combine( System.Web.HttpContext.Current.Application["appPath"].ToString(), "logs/logs.txt" );
string FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs//logs.txt");
if (File.Exists(FilePath))
{
FileInfo fi = new FileInfo(FilePath);
if (fi.Length >= FileSize)
{
//fi.Delete();//如果采用了删除之后再添加的办法,则必须要求logs文件夹要有写入的权限
StreamWriter rw = new StreamWriter(FilePath, false);
rw.Write("");
rw.Flush();
rw.Close();
}
}
byte[] buffer = Encoding.UTF8.GetBytes(TraceMsg);
StreamWriter rw2 = new StreamWriter(FilePath, true);
rw2.WriteLine(TraceMsg + " " + System.DateTime.Now.ToString("yyyy-MM-dd H:mm:ss"));
rw2.Flush();
rw2.Close();
}
catch (Exception)
{
//是否提示
}
}
}
新的日志类:
public static void LogWriteText(string TraceMsg)
{
try
{
string FileName = Utils.GetDate().Replace("-", "") + ".config";
string FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs.cs//" + FileName);
FileInfo Fi = new FileInfo(FilePath);
if (!Fi.Exists)
{
XmlOperations.CreateXmlFile(FilePath, "LogsList");
}
XmlOperations XmlDoc = new XmlOperations(FilePath);
XmlDoc.InsertNode("//LogsList", "log", null, null, "");
XmlDoc.InsertNode("/LogsList/log[last()]", "DateTime", null, null, Utils.GetDateTime());
XmlDoc.InsertNode("/LogsList/log[last()]", "Message", null, null, TraceMsg);
}
catch
{
}
}