程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成自界說windows體系日記的辦法

C#完成自界說windows體系日記的辦法

編輯:C#入門知識

C#完成自界說windows體系日記的辦法。本站提示廣大學習愛好者:(C#完成自界說windows體系日記的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成自界說windows體系日記的辦法正文


本文實例講述了C#完成自界說windows體系日記的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApp
{
 /// <summary>
 /// 體系日記
 /// </summary>
 public class PackSystemEventLog
 {
  /// <summary>
  /// 毛病信息
  /// </summary>
  private static string ErrorInfo { get; set; }
  /// <summary>
  /// 創立體系事宜日記分類
  /// </summary>
  /// <param name="eventSourceName">注冊事宜源(好比說這個日記起源於某一個運用法式)</param>
  /// <param name="logName">日記稱號(事宜列表顯示的稱號)</param>
  /// <returns></returns>
  public static bool CreateSystemEventLogCategory(string eventSourceName, string logName)
  {
   bool createResult = false;
   try
   {
    if (!EventLog.SourceExists(eventSourceName))
    {
     EventLog.CreateEventSource(eventSourceName, logName);
    }
    createResult = true;
   }
   catch (Exception ex)
   {
    createResult = false;
    ErrorInfo = ex.Message;
   }
   return createResult;
  }
  /// <summary>
  /// 刪除體系事宜日記分類
  /// </summary>
  /// <param name="eventSource">EventName事宜源</param>
  /// <returns></returns>
  public static bool RemoveSystemEventSourceCategory(string eventSource)
  {
   bool createResult = false;
   try
   {
    if (EventLog.SourceExists(eventSource))
    {
     EventLog.DeleteEventSource(eventSource, ".");
    }
    createResult = true;
   }
   catch (Exception ex)
   {
    createResult = false;
    ErrorInfo = ex.Message;
   }
   return createResult;
  }
  /// <summary>
  /// 向體系日記中寫入日記
  /// </summary>
  /// <param name="eventSource">事宜源</param>
  /// <param name="msg">寫入日記信息</param>
  /// <param name="type">日記文天職類(正告、信息、毛病)</param>
  /// <returns></returns>
  public static bool WriteSystemEventLog(string eventSource, string msg, EventLogEntryType type)
  {
   bool writeResult = false;
   try
   {
    if (!EventLog.SourceExists(eventSource))
    {
     writeResult = false;
     ErrorInfo = "日記分類不存在!";     
    }
    else
    {
     EventLog.WriteEntry(eventSource, msg, type);
     writeResult = true;
    }
   }
   catch (Exception ex)
   {
    writeResult = false;
    ErrorInfo = ex.Message;
   }
   return writeResult;
  }
  /// <summary>
  /// 刪除事宜源中logName(似乎刪除一切的該分類的日記)
  /// </summary>
  /// <param name="eventSource"></param>
  /// <param name="logName"></param>
  /// <returns></returns>
  public static bool RemoveSystemEventLog(string eventSource, string logName)
  {
   bool removeResult = false;
   try
   {
    if (!EventLog.SourceExists(eventSource))
    {
     removeResult = false;
     ErrorInfo = "日記分類不存在!";
    }
    else
    {
     EventLog.Delete(logName);
     removeResult = true;
    }
   }
   catch (Exception ex)
   {
    removeResult = false;
    ErrorInfo = ex.Message;
   }
   return removeResult;
  }
  /// <summary>
  /// 獲得毛病信息
  /// </summary>
  /// <returns></returns>
  public static string GetErrorMessage()
  {
   return ErrorInfo;
  }
 }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved