C#完成寫體系日記的辦法。本站提示廣大學習愛好者:(C#完成寫體系日記的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成寫體系日記的辦法正文
本文實例講述了C#完成寫體系日記的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
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#法式設計有所贊助。