在C#下面我想实现类似于C的写Log文件功能:
FILE *p=fopen("log.txt", "w");
fprintf(p, "data information");
fclose(p);不知道怎么实现?
FILE *p=fopen("log.txt", "w");
fprintf(p, "data information");
fclose(p);不知道怎么实现?
解决方案 »
- skeleton的 position 的X,Y 是相对于什么的距离
- 为什么一段程序在timer Tick 外可以运行,在Timer Tick 内倒不行了?
- 将D盘上的一个目录cst备份到H盘上,D盘上的cst目录中的文件是经常变化的,请问如何实现D:\cst和H:\cst这两个目录的自动定期同步备份
- dataGridView 问题
- 使用DataGridView时在行验证事件中如何能删除被验证的行?
- 用户密码的修改
- 现在我对图片进行旋转,图片是放在PictureBox中的,但图片模糊了
- 怎样托动一个没有标题栏的窗体!!!!100分
- 请给个下载可用的现成词库的下载,谢谢!
- 关于C#做统计图形的问题
- C#里如何使用RTC?
- string [] Split用法 急!
public class Log
{
public Log(string LogStr)
{
StreamWriter sw = null;
try
{
LogStr = DateTime.Now.ToLocalTime().ToString() +"\n" + LogStr;
sw = new StreamWriter("D:\\Log.txt", true);
sw.WriteLine(LogStr);
}
catch
{
}
finally
{
if (sw != null)
{
sw.Close();
}
}
}
}
//写错误日志
if (ErrList.Count>0)
{
if (!EventLog.SourceExists("ServerSource"))
{
EventLog.CreateEventSource("ServerSource", "Log");
} EventLog myLog = new EventLog();
myLog.Source = "ServerSource"; for (int i = 0; i < ErrList.Count; i++)
{
//写入系统日志里
myLog.WriteEntry(ErrList[i].ToString()); //写日志文件
using (StreamWriter w = File.AppendText("log.txt"))
{
w.Write("\r\n日期 : ");
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
w.WriteLine(" ");
w.WriteLine("日志描述:{0}", ErrList[i].ToString());
w.WriteLine("-------------------------------");
w.Flush(); }
}
}
功能绝对强大
http://www.cnblogs.com/liwei6797/archive/2007/04/27/729679.html详细介绍了log4net很强大的样子~
=====
同意。
这个比log4net简单,更加适合小项目。如果你的项目比较大,log4net也不是很合适,应该采用CodeSite或者SmartInspect。
public static void WriteErrorMessageToXmlFile(string errorDate, string errorMessage)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XmlOperate.xmlPath); XmlNode root = xmlDoc.SelectSingleNode("Error");
XmlElement xeErrorItem = xmlDoc.CreateElement("ErrorItem"); XmlElement xeDate = xmlDoc.CreateElement("Date");
xeDate.InnerText = errorDate;
xeErrorItem.AppendChild(xeDate); XmlElement xeMessage = xmlDoc.CreateElement("Message");
xeMessage.InnerText = errorMessage;
xeErrorItem.AppendChild(xeMessage); root.AppendChild(xeErrorItem); xmlDoc.Save(XmlOperate.xmlPath);}<?xml version="1.0" encoding="utf-8"?>
<!--记录应用程序的出错信息-->
<Error>
<ErrorItem>
<Date>2007-7-18 19:33:38</Date>
<Message>测试第一个错误,哈哈!</Message>
</ErrorItem>
</Error>
仅供参考