首页 > 编程 > 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. { 
  8. /// <summary> 
  9. /// 系统日志 
  10. /// </summary> 
  11. public class PackSystemEventLog 
  12. { 
  13. /// <summary> 
  14. /// 错误信息 
  15. /// </summary> 
  16. private static string ErrorInfo { get; set; } 
  17. /// <summary> 
  18. /// 创建系统事件日志分类 
  19. /// </summary> 
  20. /// <param name="eventSourceName">注册事件源(比如说这个日志来源于某一个应用程序)</param> 
  21. /// <param name="logName">日志名称(事件列表显示的名称)</param> 
  22. /// <returns></returns> 
  23. public static bool CreateSystemEventLogCategory(string eventSourceName, string logName) 
  24. { 
  25. bool createResult = false; 
  26. try 
  27. { 
  28. if (!EventLog.SourceExists(eventSourceName)) 
  29. { 
  30. EventLog.CreateEventSource(eventSourceName, logName); 
  31. } 
  32. createResult = true; 
  33. } 
  34. catch (Exception ex) 
  35. { 
  36. createResult = false; 
  37. ErrorInfo = ex.Message; 
  38. } 
  39. return createResult; 
  40. } 
  41. /// <summary> 
  42. /// 删除系统事件日志分类 
  43. /// </summary> 
  44. /// <param name="eventSource">EventName事件源</param> 
  45. /// <returns></returns> 
  46. public static bool RemoveSystemEventSourceCategory(string eventSource) 
  47. { 
  48. bool createResult = false; 
  49. try 
  50. { 
  51. if (EventLog.SourceExists(eventSource)) 
  52. { 
  53. EventLog.DeleteEventSource(eventSource, "."); 
  54. } 
  55. createResult = true; 
  56. } 
  57. catch (Exception ex) 
  58. { 
  59. createResult = false; 
  60. ErrorInfo = ex.Message; 
  61. } 
  62. return createResult; 
  63. } 
  64. /// <summary> 
  65. /// 向系统日志中写入日志 
  66. /// </summary> 
  67. /// <param name="eventSource">事件源</param> 
  68. /// <param name="msg">写入日志信息</param> 
  69. /// <param name="type">日志文本分类(警告、信息、错误)</param> 
  70. /// <returns></returns> 
  71. public static bool WriteSystemEventLog(string eventSource, string msg, EventLogEntryType type) 
  72. { 
  73. bool writeResult = false; 
  74. try 
  75. { 
  76. if (!EventLog.SourceExists(eventSource)) 
  77. { 
  78. writeResult = false; 
  79. ErrorInfo = "日志分类不存在!";  
  80. } 
  81. else 
  82. { 
  83. EventLog.WriteEntry(eventSource, msg, type); 
  84. writeResult = true; 
  85. } 
  86. } 
  87. catch (Exception ex) 
  88. { 
  89. writeResult = false; 
  90. ErrorInfo = ex.Message; 
  91. } 
  92. return writeResult; 
  93. } 
  94. /// <summary> 
  95. /// 删除事件源中logName(好像删除了所有的该分类的日志) 
  96. /// </summary> 
  97. /// <param name="eventSource"></param> 
  98. /// <param name="logName"></param> 
  99. /// <returns></returns> 
  100. public static bool RemoveSystemEventLog(string eventSource, string logName) 
  101. { 
  102. bool removeResult = false; 
  103. try 
  104. { 
  105. if (!EventLog.SourceExists(eventSource)) 
  106. { 
  107. removeResult = false; 
  108. ErrorInfo = "日志分类不存在!"; 
  109. } 
  110. else 
  111. { 
  112. EventLog.Delete(logName); 
  113. removeResult = true; 
  114. } 
  115. } 
  116. catch (Exception ex) 
  117. { 
  118. removeResult = false; 
  119. ErrorInfo = ex.Message; 
  120. } 
  121. return removeResult; 
  122. } 
  123. /// <summary> 
  124. /// 获取错误信息 
  125. /// </summary> 
  126. /// <returns></returns> 
  127. public static string GetErrorMessage() 
  128. { 
  129. return ErrorInfo; 
  130. } 
  131. } 
  132. } 

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表