- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using System.IO;
- namespace IISAppPoolRestart
- {
- class Log
- {
- #region 系统事件日志
- ///
- /// 建立系统事件日志
- ///
- /// 事件源
- /// 事件名称
- /// 建立是否成功
- public bool CreateEventLog(string LogSource, string LogName)
- {
- bool rel = false;
- if (!EventLog.SourceExists(LogName))
- {
- EventLog.CreateEventSource(LogSource, LogName);
- rel = true;
- }
- return rel;
- }
- ///
- /// 写入事件日志
- ///
- /// 事件源
- /// 事件名称
- /// 事件内容
- 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 文件日志
- ///
- /// 写文件
- ///
- /// 文件路径
- /// 写入内容
- 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();
- }
- ///
- /// 写文件,包括时间
- ///
- /// 文件路径
- /// 写入内容
- /// 写入时间
- 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
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0704201512101.html
来源: http://www.codesnippet.cn/detail/0704201512101.html