Log.cs类

一个处理信息的并,能把信息,错误,警告添加到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

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值