C# 日志记录类
来源:网络来源TAG:日志记录类浏览数: 日期:2010-7-27评论:
折叠C# Code复制内容到剪贴板
  1. //日志记录类   
  2.  using System;   
  3.  using System.Configuration;   
  4.  using System.Diagnostics;   
  5.  using System.IO;   
  6.  using System.Text;   
  7.  using System.Threading;   
  8.   
  9.  namespace MyEventLog   
  10.  {   
  11.   /// <summary>   
  12.   /// 事件日志记录类,提供事件日志记录支持    
  13.   /// <remarks>   
  14.   /// 定义了4个日志记录方法 (error, warning, info, trace)    
  15.   /// </remarks>   
  16.   /// </summary>   
  17.   public class ApplicationLog   
  18.   {   
  19.    /// <summary>   
  20.    /// 将错误信息记录到Win2000/NT事件日志中   
  21.    /// <param name="message">需要记录的文本信息</param>   
  22.    /// </summary>   
  23.    public static void WriteError(String message)   
  24.    {   
  25.     WriteLog(TraceLevel.Error, message);   
  26.    }   
  27.   
  28.    /// <summary>   
  29.    /// 将警告信息记录到Win2000/NT事件日志中   
  30.    /// <param name="message">需要记录的文本信息</param>   
  31.    /// </summary>   
  32.    public static void WriteWarning(String message)   
  33.    {   
  34.     WriteLog(TraceLevel.Warning, message);     
  35.    }   
  36.   
  37.    /// <summary>   
  38.    /// 将提示信息记录到Win2000/NT事件日志中   
  39.    /// <param name="message">需要记录的文本信息</param>   
  40.    /// </summary>   
  41.    public static void WriteInfo(String message)   
  42.    {   
  43.     WriteLog(TraceLevel.Info, message);   
  44.    }   
  45.    /// <summary>   
  46.    /// 将跟踪信息记录到Win2000/NT事件日志中   
  47.    /// <param name="message">需要记录的文本信息</param>   
  48.    /// </summary>   
  49.    public static void WriteTrace(String message)   
  50.    {   
  51.     WriteLog(TraceLevel.Verbose, message);   
  52.    }   
  53.   
  54.    /// <summary>   
  55.    /// 格式化记录到事件日志的文本信息格式   
  56.    /// <param name="ex">需要格式化的异常对象</param>   
  57.    /// <param name="catchInfo">异常信息标题字符串.</param>   
  58.    /// <retvalue>   
  59.    /// <para>格式后的异常信息字符串,包括异常内容和跟踪堆栈.</para>   
  60.    /// </retvalue>   
  61.    /// </summary>   
  62.    public static String FormatException(Exception ex, String catchInfo)   
  63.    {   
  64.     StringBuilder strBuilder = new StringBuilder();   
  65.     if (catchInfo != String.Empty)   
  66.     {   
  67.      strBuilder.Append(catchInfo).Append("\r\n");   
  68.     }   
  69.     strBuilder.Append(ex.Message).Append("\r\n").Append(ex.StackTrace);   
  70.     return strBuilder.ToString();   
  71.    }   
  72.   
  73.    /// <summary>   
  74.    /// 实际事件日志写入方法   
  75.    /// <param name="level">要记录信息的级别(error,warning,info,trace).</param>   
  76.    /// <param name="messageText">要记录的文本.</param>   
  77.    /// </summary>   
  78.    private static void WriteLog(TraceLevel level, String messageText)   
  79.    {   
  80.     try  
  81.     {    
  82.      EventLogEntryType LogEntryType;   
  83.      switch (level)   
  84.      {   
  85.       case TraceLevel.Error:   
  86.        LogEntryType = EventLogEntryType.Error;   
  87.        break;   
  88.       case TraceLevel.Warning:   
  89.        LogEntryType = EventLogEntryType.Warning;   
  90.        break;   
  91.       case TraceLevel.Info:   
  92.        LogEntryType = EventLogEntryType.Information;   
  93.        break;   
  94.       case TraceLevel.Verbose:   
  95.        LogEntryType = EventLogEntryType.SuccessAudit;   
  96.        break;   
  97.       default:   
  98.        LogEntryType = EventLogEntryType.SuccessAudit;   
  99.        break;   
  100.      }   
  101.   
  102.      EventLog eventLog = new EventLog("Application", ApplicationConfiguration.EventLogMachineName, ApplicationConfiguration.EventLogSourceName );   
  103.      //写入事件日志   
  104.      eventLog.WriteEntry(messageText, LogEntryType);   
  105.   
  106.     }   
  107.    catch {} //忽略任何异常   
  108.   }    
  109.  } //class ApplicationLog   
  110. }   
昨天新闻点击排行
一周新闻点击排行
当月新闻点击排行
相关评论
正在加载评论列表...
评论表单加载中...