C#实现自定义windows系统日志的方法

时间:2022-02-12 11:14:03

本文实例讲述了C#实现自定义windows系统日志的方法。分享给大家供大家参考。具体实现方法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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#程序设计有所帮助。