啥也不说啦,放代码: using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.IO; namespace IISAppPoolRestart { class Log { #region 系统事件日志 /// <summary> /// 建立系统事件日志 /// </summary> /// <param name="LogSource">事件源</param> /// <param name="LogName">事件名称</param> /// <returns>建立是否成功</returns> public bool CreateEventLog(string LogSource, string LogName) { bool rel = false; if (!EventLog.SourceExists(LogName)) { EventLog.CreateEventSource(LogSource, LogName); rel = true; } return rel; } /// <summary> /// 写入事件日志 /// </summary> /// <param name="LogSource">事件源</param> /// <param name="LogName">事件名称</param> /// <param name="Log">事件内容</param> public void RecordSysLog(string LogSource, string LogName, string Log) { if (!EventLog.SourceExists(LogSource)) { //CreateEventLog创建方法参数说明:LogSource-在内部使用的名称,即日志源(删除时要选择这个);LogName为日志名称 if (this.CreateEventLog(LogSource, LogName)) { EventLog elog1 = new EventLog(LogName); elog1.WriteEntry(Log); elog1.Close(); } } } #endregion #region 文件日志 /// <summary> /// 写文件 /// </summary> /// <param name="Path">文件路径</param> /// <param name="Strings">写入内容</param> public static void WriteFile(string Path, string Strings) { if (!File.Exists(Path)) { FileStream f = File.Create(Path); f.Close(); f.Dispose(); } StreamWriter f1 = new StreamWriter(Path, true, Encoding.UTF8); f1.WriteLine(Strings); f1.Close(); f1.Dispose(); } /// <summary> /// 写文件,包括时间 /// </summary> /// <param name="Path">文件路径</param> /// <param name="Strings">写入内容</param> /// <param name="dt">写入时间</param> public static void WriteFile(string Path, string Strings, DateTime dt) { if (!File.Exists(Path)) { FileStream f = File.Create(Path); f.Close(); f.Dispose(); } StreamWriter f2 = new StreamWriter(Path, true, Encoding.UTF8); f2.WriteLine(Strings + " | " + dt.ToLongDateString()); f2.Close(); f2.Dispose(); } #endregion } }