一个处理信息的并,能把信息,错误,警告添加到Event View的类
using System;
using System.IO;
using System.Diagnostics;
using System.Configuration;
namespace Common
{
/// <summary>
/// Summary description for Logs.
/// </summary>
public class Logs
{
/// <summary>
/// 错误信息加入Event View的方法
/// </summary>
/// <param name="ErrorType">错误信息类型:"E"表示错误,"W"表示警告,"I"表示信息</param>
/// <param name="ErrorMessage"></param>
public void EventLog(String ErrorType,String ErrorMessage )
{
string TraceConfig;
string EventLogName;
EventLogName = "EventLogName";
System.Diagnostics.EventLog objEventLog = new EventLog();
objEventLog.Source = "EventLogName";
if(!System.Diagnostics.EventLog.SourceExists(EventLogName))
{
System.Diagnostics.EventLog.CreateEventSource(EventLogName,EventLogName);
}
try
{
//从app.config文件中获得Trace Mode的值
//如果是Info,则Event View中加载任何信息
//如果Error,则Event View中仅加载错误,和警告信息
//如果Warn,则Event View中仅加载错误信息
TraceConfig = ConfigurationSettings.AppSettings["Trace Mode"];
}
catch
{
TraceConfig = "INFO";
}
try
{
if(TraceConfig.ToUpper() == "INFO")
{
if(ErrorType == "E")
{
objEventLog.WriteEntry(System.DateTime.Now.ToString() + " " + ErrorMessage , EventLogEntryType.Error);
WriteLogToFile(System.DateTime.Now.ToString() + " " + ErrorMessage);
}
else if(ErrorType == "W")
{
objEventLog.WriteEntry(System.DateTime.Now.ToString() + " " + ErrorMessage , EventLogEntryType.Warning);
}
else if(ErrorType == "I")
{
objEventLog.WriteEntry(System.DateTime.Now.ToString() + " " + ErrorMessage , EventLogEntryType.Information);
}
}//Info
else if(TraceConfig.ToUpper() == "WARNING")
{
if(ErrorType == "E")
{
objEventLog.WriteEntry(System.DateTime.Now.ToString() + " " + ErrorMessage , EventLogEntryType.Error);
WriteLogToFile(System.DateTime.Now.ToString() + " " + ErrorMessage);
}
else if(ErrorType == "W")
{
objEventLog.WriteEntry(System.DateTime.Now.ToString() + " " + ErrorMessage , EventLogEntryType.Warning);
}
}//Warning
else if(TraceConfig.ToUpper() == "ERROR")
{
if(ErrorType == "E")
{
objEventLog.WriteEntry(System.DateTime.Now.ToString() + " " + ErrorMessage , EventLogEntryType.Error);
WriteLogToFile(System.DateTime.Now.ToString() + " " + ErrorMessage);
}
}//Error
else
{
objEventLog.WriteEntry(System.DateTime.Now.ToString() + " " + ErrorMessage , EventLogEntryType.Error);
}//try
}
catch
{
//do nothing
}
}
/// <summary>
/// 将日志写入文件
/// </summary>
/// <param name="ErrorMessage"></param>
private void WriteLogToFile(string ErrorMessage)
{
try
{
string logfilepath = "C://Documents and Settings";//ConfigurationSettings.AppSettings[""];//--------app.config fildes unknow
string machinename = Environment.UserName.ToString();//ConfigurationSettings.AppSettings[""];//--------app.config fildes unknow
string applicationid = "Desktop";//ConfigurationSettings.AppSettings[""];//--------app.config fildes unknow
string currentpath = logfilepath;
DirectoryInfo di = new DirectoryInfo(currentpath);
if (di.Exists == true)
{
currentpath = currentpath + "//" +machinename;
di = new DirectoryInfo(currentpath);
if(di.Exists == true)
{
//do nothing
}
else
{
di.Create();
}
currentpath = currentpath +"//" + applicationid;
di = new DirectoryInfo(currentpath);
if(di.Exists == true)
{
//do nothing
}
else
{
di.Create();
}
currentpath = currentpath + "//" +"error.txt";
StreamWriter stw = new StreamWriter(currentpath,false);
stw.WriteLine(System.DateTime.Now.ToString() + " " + ErrorMessage);
stw.Close();
}
}
catch
{
//do nothing
}
}
}//class Logs
}