国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現自定義windows系統日志的方法

2019-10-29 21:39:12
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了C#實現自定義windows系統日志的方法,涉及C#針對windows系統日志的創建、讀寫及刪除技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了C#實現自定義windows系統日志的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Diagnostics; 
  6. namespace ConsoleApp 
  7. /// <summary> 
  8. /// 系統日志 
  9. /// </summary> 
  10. public class PackSystemEventLog 
  11. /// <summary> 
  12. /// 錯誤信息 
  13. /// </summary> 
  14. private static string ErrorInfo { get; set; } 
  15. /// <summary> 
  16. /// 創建系統事件日志分類 
  17. /// </summary> 
  18. /// <param name="eventSourceName">注冊事件源(比如說這個日志來源于某一個應用程序)</param> 
  19. /// <param name="logName">日志名稱(事件列表顯示的名稱)</param> 
  20. /// <returns></returns> 
  21. public static bool CreateSystemEventLogCategory(string eventSourceName, string logName) 
  22. bool createResult = false
  23. try 
  24. if (!EventLog.SourceExists(eventSourceName)) 
  25. EventLog.CreateEventSource(eventSourceName, logName); 
  26. createResult = true
  27. catch (Exception ex) 
  28. createResult = false
  29. ErrorInfo = ex.Message; 
  30. return createResult; 
  31. /// <summary> 
  32. /// 刪除系統事件日志分類 
  33. /// </summary> 
  34. /// <param name="eventSource">EventName事件源</param> 
  35. /// <returns></returns> 
  36. public static bool RemoveSystemEventSourceCategory(string eventSource) 
  37. bool createResult = false
  38. try 
  39. if (EventLog.SourceExists(eventSource)) 
  40. EventLog.DeleteEventSource(eventSource, "."); 
  41. createResult = true
  42. catch (Exception ex) 
  43. createResult = false
  44. ErrorInfo = ex.Message; 
  45. return createResult; 
  46. /// <summary> 
  47. /// 向系統日志中寫入日志 
  48. /// </summary> 
  49. /// <param name="eventSource">事件源</param> 
  50. /// <param name="msg">寫入日志信息</param> 
  51. /// <param name="type">日志文本分類(警告、信息、錯誤)</param> 
  52. /// <returns></returns> 
  53. public static bool WriteSystemEventLog(string eventSource, string msg, EventLogEntryType type) 
  54. bool writeResult = false
  55. try 
  56. if (!EventLog.SourceExists(eventSource)) 
  57. writeResult = false
  58. ErrorInfo = "日志分類不存在!";  
  59. else 
  60. EventLog.WriteEntry(eventSource, msg, type); 
  61. writeResult = true
  62. catch (Exception ex) 
  63. writeResult = false
  64. ErrorInfo = ex.Message; 
  65. return writeResult; 
  66. /// <summary> 
  67. /// 刪除事件源中logName(好像刪除了所有的該分類的日志) 
  68. /// </summary> 
  69. /// <param name="eventSource"></param> 
  70. /// <param name="logName"></param> 
  71. /// <returns></returns> 
  72. public static bool RemoveSystemEventLog(string eventSource, string logName) 
  73. bool removeResult = false
  74. try 
  75. if (!EventLog.SourceExists(eventSource)) 
  76. removeResult = false
  77. ErrorInfo = "日志分類不存在!"
  78. else 
  79. EventLog.Delete(logName); 
  80. removeResult = true
  81. catch (Exception ex) 
  82. removeResult = false
  83. ErrorInfo = ex.Message; 
  84. return removeResult; 
  85. /// <summary> 
  86. /// 獲取錯誤信息 
  87. /// </summary> 
  88. /// <returns></returns> 
  89. public static string GetErrorMessage() 
  90. return ErrorInfo; 

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 土默特右旗| 响水县| 临桂县| 文昌市| 开阳县| 华容县| 南宫市| 长春市| 巨野县| 鸡东县| 肇州县| 房产| 乌兰察布市| 冕宁县| 广德县| 绍兴县| 固阳县| 海安县| 固安县| 儋州市| 郸城县| 肇庆市| 新巴尔虎左旗| 呈贡县| 景宁| 汾西县| 阿尔山市| 新兴县| 凤凰县| 无极县| 班玛县| 旬邑县| 平利县| 绥江县| 遵义县| 玉树县| 金门县| 新乡市| 讷河市| 桃园县| 元朗区|