本文實例講述了C#實現自定義windows系統日誌的方法。分享給大家供大家參考。具體實現方法如下:
using System;using ric;using ;using ;using nostics;namespace ConsoleApp{ ///
/// 系統日誌 ///
public class PackSystemEventLog { ///
/// 錯誤信息 ///
private static string ErrorInfo { get; set; } ///
/// 創建系統事件日誌分類 ///
///
註冊事件源(比如說這個日誌來源於某一個應用程序)///
日誌名稱(事件列表顯示的名稱)///
public static bool CreateSystemEventLogCategory(string eventSourceName, string logName) { bool createResult = false; try { if (!ceExists(eventSourceName)) { teEventSource(eventSourceName, logName); } createResult = true; } catch (Exception ex) { createResult = false; ErrorInfo = age; } return createResult; } ///
/// 刪除系統事件日誌分類 ///
///
EventName事件源///
public static bool RemoveSystemEventSourceCategory(string eventSource) { bool createResult = false; try { if (ceExists(eventSource)) { teEventSource(eventSource, "."); } createResult = true; } catch (Exception ex) { createResult = false; ErrorInfo = age; } return createResult; } ///
/// 向系統日誌中寫入日誌 ///
///
事件源///
寫入日誌信息///
日誌文本分類(警告、信息、錯誤)///
public static bool WriteSystemEventLog(string eventSource, string msg, EventLogEntryType type) { bool writeResult = false; try { if (!ceExists(eventSource)) { writeResult = false; ErrorInfo = "日誌分類不存在!"; } else { eEntry(eventSource, msg, type); writeResult = true; } } catch (Exception ex) { writeResult = false; ErrorInfo = age; } return writeResult; } ///
/// 刪除事件源中logName(好像刪除了所有的`該分類的日誌) ///
///
///
///
public static bool RemoveSystemEventLog(string eventSource, string logName) { bool removeResult = false; try { if (!ceExists(eventSource)) { removeResult = false; ErrorInfo = "日誌分類不存在!"; } else { te(logName); removeResult = true; } } catch (Exception ex) { removeResult = false; ErrorInfo = age; } return removeResult; } ///
/// 獲取錯誤信息 ///
///
public static string GetErrorMessage() { return ErrorInfo; } }}
希望本文所述對大家的C#程序設計有所幫助。