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

 更新时间:2020年6月25日 11:17  点击:1577

因为我不想使用自己写文件,我的软件是绿色的,所以把日志写到 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;
  }
 }
}

[!--infotagslink--]

相关文章

  • C# winform实现右下角弹出窗口结果的方法

    这篇文章主要介绍了C# winform实现右下角弹出窗口结果的方法,结合实例形式分析了C#窗口操作的相关技巧,需要的朋友可以参考下...2020-06-25
  • C#实现BBcode转为Markdown的方法

    这篇文章主要给大家介绍了关于C#实现BBcode转Markdown的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。...2020-06-25
  • 使用C#实现写入系统日志

    本文给大家分享的是作者使用使用C#实现将软件日志写入系统日志中的方法,十分巧妙,有需要的小伙伴可以参考下...2020-06-25
  • log4net创建系统日志的详细步骤

    log4net是.Net下一个非常优秀的开源日志记录组件。log4net记录日志的功能非常强大。它可以将日志分不同的等级,以不同的格式,输出到不同的媒介。本文主要是简单的介绍如何在Visual Studio2010(Asp.Net Mvc3.0)中使用log4net快速创建系统日志,如何扩展以输出自定义字段...2021-09-22