您的位置:首页 > 其它

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

2018-01-14 08:59 861 查看

因为我不想使用自己写文件,我的软件是绿色的,所以把日志写到 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# 系统日志
相关文章推荐