CSDN博客

img bluesky35

[原创]在windows2000中使用.NET写日志

发表于2004/9/23 16:13:00  1362人阅读

分类: .NET技术专栏

                                                                                   作者:bluesky35(蓝天)

1.首先请参阅其他技术专栏中用VB6.0访问注册表,创建

HKEY_LOCAL_MACHINE,/SYSTEM/CurrentControlSet/Services/Eventlog/Eventlog2/Eventlog2
键,并且写入EventMessageFile,value为C:/WINNT/Microsoft.NET/Framework/v1.1.4322/EventLogMessages.dll

2.在C#.net使用以下代码写日志
//输出日志的CLASS
using System;
using System.Diagnostics;
using System.Text;

namespace CorePlus.Framework.Utility
{
 /// <summary>
 /// 写日志的CLASS
 /// </summary>
 public class LogUtility
 {
  public enum EVENT
  {
   /// <summary>重大错误</summary>
   EVENT_ERROR,
   /// <summary>操作失败</summary>
   EVENT_FAILUREAUDIT,
   /// <summary>一般信息</summary>
   EVENT_INFORMATION,
   /// <summary>有效的,成功的操作</summary>
   EVENT_SUCCESSAUDIT,
   /// <summary>警告</summary>
   EVENT_WARNING,
  }

  private static TextWriterTraceListener listner = null;

  /// <summary>
  /// 构造函数
  /// </summary>
  public LogUtility () {}

  /// <param name="loginID">登录ID</param>
  /// <param name="screenID">画面ID</param>
  /// <param name="level">警告级别</param>
  /// <param name="message">日志信息</param>
  public static void Logging (
   string loginID,
   string screenID,
   EVENT level,
   string message )
  {
   // 变量定义
   StringBuilder  outputMessage = null; // 输出的信息
   string    targetLevelString = null;
   EventLogEntryType Eventtype = new EventLogEntryType();

   switch (level)
   {
    case EVENT.EVENT_ERROR:
     Eventtype = EventLogEntryType.Error;
     break;

    case EVENT.EVENT_FAILUREAUDIT:
     Eventtype = EventLogEntryType.FailureAudit;
     break;

    case EVENT.EVENT_INFORMATION:
     Eventtype = EventLogEntryType.Information;
     break;

    case EVENT.EVENT_SUCCESSAUDIT:
     Eventtype = EventLogEntryType.SuccessAudit;
     break;

    case EVENT.EVENT_WARNING:
     Eventtype = EventLogEntryType.Warning;
     break;
   }

   // 日志信息的拼合
   outputMessage = MakeMessage( loginID, screenID, targetLevelString, message );

   // 写日志
   Logging( Eventtype, outputMessage.ToString() );
  }

  /// <summary>
  /// 向日志管理器写日志
  /// </summary>
  /// <param name="level">错误级别</param>
  /// <param name="message">输出的信息</param>
  private static void Logging ( EventLogEntryType level, string message )
  {
   // 变量定义
   string  logName  = null; // 日志名
   string  machineName = null; // 机器名
   string  sourceName = null; // SourceName
   EventLog eventLog = null; // EventLog

   logName  = "Eventlog2";  // 日志名
   machineName = "."; // 机器名
   sourceName = "Eventlog2"; // SourceName

   eventLog = new EventLog( logName, machineName, sourceName );
   eventLog.WriteEntry( message,level );
   eventLog = null;
  }

  /// <summary>
  /// 日志最终拼合
  /// </summary>
  /// <param name="loginID">登录ID</param>
  /// <param name="screenID">画面ID</param>
  /// <param name="levelString">错误级别</param>
  /// <param name="message">日志信息</param>
  /// <returns>输出的日志信息</returns>
  private static StringBuilder MakeMessage (
   string loginID,
   string screenID,
   string levelString,
   string message )
  {
   // 变量定义
   StringBuilder retMessage = new StringBuilder();

   retMessage.Append( "【登录ID】" );
   retMessage.Append( loginID );
   retMessage.Append( "/n【画面ID】" );
   retMessage.Append( screenID );
   retMessage.Append( "/n【错误级别】" );
   retMessage.Append( levelString );
   retMessage.Append( "/n【错误信息】/n" );
   retMessage.Append( message );

   return retMessage;
    }
  }
}

// 在FORM中输出日志
  private void button1_Click(object sender, System.EventArgs e)
  {
   LogUtility.Logging( "bbbbbb","22222222",LogUtility.EVENT.EVENT_ERROR,"3333333" );
  }

运行完程序后打开日志管理器看看~~~~~~~~~怎么样?不错吧,呵呵.

0 0

相关博文

我的热门文章

img
取 消
img