使用C#实现写入系统日志

时间:2021-11-02 07:05:11

因为我不想使用自己写文件,我的软件是绿色的,所以把日志写到 windows 日志。

首先告诉大家什么是系统日志,请看下面

使用C#实现写入系统日志

如果需要写日志,需要管理员权限,如果没有权限会出现下面异常

?
1
system.security.securityexception:“未找到源,但未能搜索某些或全部事件日志。 不可访问的日志: security

需要判断当前是否已经存在日志,下面我来创建一个事件叫 “德熙”

?
1
2
3
4
if (eventlog.sourceexists("德熙"))
  {
  eventlog.createeventsource("德熙", "application");
  }

这里的 application 就是写到哪个,一般都是选 application ,可以从图片看到系统的有应用程序、安全、setup、系统几个日志,程序一般都是写到程序

写日志

写日志就不用管理权限

写入可以使用 writeentry ,需要传入写入的日志和内容

?
1
eventlog.writeentry("德熙", "有个不愿告诉你名称的程序在这里写字符串");

这个方法还有几个重载,可以传入日志类型,是成功、失败还是其他。还可以传入 id ,通过id 可以找到为什么需要写日志,不过需要在自己定义,还可以添加附件,于是我就不需要自己写文件日志。

使用C#实现写入系统日志

另外给大家附上一个完整例子

?
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;
  }
 }
}

原文链接:https://lindexi.github.io/lindexi/post/C-写系统日志.html