本文给大家分享的是作者使用使用C#实现将软件日志写入系统日志中的方法,十分巧妙,有需要的小伙伴可以参考下
因为我不想使用自己写文件,我的软件是绿色的,所以把日志写到 Windows 日志。
首先告诉大家什么是系统日志,请看下面
如果需要写日志,需要管理员权限,如果没有权限会出现下面异常
System.Security.SecurityException:“未找到源,但未能搜索某些或全部事件日志。 不可访问的日志: Security
需要判断当前是否已经存在日志,下面我来创建一个事件叫 “德熙”
if (EventLog.SourceExists("德熙"))
{
EventLog.CreateEventSource("德熙", "Application");
}
这里的 Application 就是写到哪个,一般都是选 Application ,可以从图片看到系统的有应用程序、安全、Setup、系统几个日志,程序一般都是写到程序
写日志
写日志就不用管理权限
写入可以使用 WriteEntry ,需要传入写入的日志和内容
EventLog.WriteEntry("德熙", "有个不愿告诉你名称的程序在这里写字符串");
这个方法还有几个重载,可以传入日志类型,是成功、失败还是其他。还可以传入 id ,通过id 可以找到为什么需要写日志,不过需要在自己定义,还可以添加附件,于是我就不需要自己写文件日志。
另外给大家附上一个完整例子
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#实现写入系统日志


基础教程推荐
猜你喜欢
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#中的Linq to JSON操作详解 2023-06-08
- Unity shader实现高斯模糊效果 2023-01-16
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C# 解析XML和反序列化的示例 2023-04-14