事件日志:
使用EventLog 类,如:
下面的示例创建源 MySource(如果尚未存在),并在事件日志 MyNewLog 中写入一项。
using System;
using System.Diagnostics;
using System.Threading;
class MySample{
public static void Main(){
// Create the source, if it does not already exist.
if(!EventLog.SourceExists( "MySource ")){
EventLog.CreateEventSource( "MySource ", "MyNewLog ");
Console.WriteLine( "CreatingEventSource ");
}
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource ";
// Write an informational entry to the event log.
myLog.WriteEntry( "Writing to event log. ");
}
}
文本文件:
如下代码向一个文本文件写入字符内容
using System;
using System.IO;
class Test
{
public static void Main()
{
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter( "TestFile.txt "))
{
// Add some text to the file.
sw.Write( "This is the ");
sw.WriteLine( "header for the file. ");
sw.WriteLine( "------------------- ");
// Arbitrary objects can also be written to the file.
sw.Write( "The date is: ");
sw.WriteLine(DateTime.Now);
}
}
}
/// <summary>
/// 生成系统日志。
/// data: 2005-10-07
/// description: 由于在写入数据的时候报IO错误,因此暂时停止日志写入
/// author: chenlihua
/// </summary>
/// <param name="Description">所记录日志描述。</param>
/// <param name="Content">所记录日志内容。</param>
public static void CreateSqlLog(string Description,string Content)
{
/*
string strTmp ="";
string FilePath = @"e:\syslog\sells_log\att_log\";
strTmp = DateTime.Now.Year.ToString()+"\\" +DateTime.Now.Month.ToString()+"\\"+DateTime.Now.Day.ToString()+"\\";
FilePath += strTmp;
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}// 文件夹操作完成。
FilePath += "Att_log_";
FilePath += DateTime.Now.Year.ToString()+"_"+DateTime.Now.Month.ToString()+"_"+DateTime.Now.Day.ToString() + "_"+DateTime.Now.Hour.ToString()+".txt";
StreamWriter sw;
if (!File.Exists(FilePath))
{
sw = File.CreateText(FilePath);
}
else
{
sw = File.AppendText(FilePath);
}
sw.WriteLine("[system time]: "+DateTime.Now.ToString());
sw.WriteLine("---------------------------------------------------------------------------------------");
sw.WriteLine(Description.ToString() + ":" + Content.ToString());
sw.WriteLine();
sw.Close();
*/
}