荟萃馆

位置:首页 > 计算机 > C语言

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

C语言9.04K

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

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

using System;using ric;using ;using ;using nostics;namespace ConsoleApp{ ///

/// 系统日志 ///

public class PackSystemEventLog { ///

/// 错误信息 ///

private static string ErrorInfo { get; set; } ///

/// 创建系统事件日志分类 ///

///

注册事件源(比如说这个日志来源于某一个应用程序)///

日志名称(事件列表显示的名称)///

public static bool CreateSystemEventLogCategory(string eventSourceName, string logName) { bool createResult = false; try { if (!ceExists(eventSourceName)) { teEventSource(eventSourceName, logName); } createResult = true; } catch (Exception ex) { createResult = false; ErrorInfo = age; } return createResult; } ///

/// 删除系统事件日志分类 ///

///

EventName事件源///

public static bool RemoveSystemEventSourceCategory(string eventSource) { bool createResult = false; try { if (ceExists(eventSource)) { teEventSource(eventSource, "."); } createResult = true; } catch (Exception ex) { createResult = false; ErrorInfo = age; } return createResult; } ///

/// 向系统日志中写入日志 ///

///

事件源///

写入日志信息///

日志文本分类(警告、信息、错误)///

public static bool WriteSystemEventLog(string eventSource, string msg, EventLogEntryType type) { bool writeResult = false; try { if (!ceExists(eventSource)) { writeResult = false; ErrorInfo = "日志分类不存在!"; } else { eEntry(eventSource, msg, type); writeResult = true; } } catch (Exception ex) { writeResult = false; ErrorInfo = age; } return writeResult; } ///

/// 删除事件源中logName(好像删除了所有的`该分类的日志) ///

///

///

///

public static bool RemoveSystemEventLog(string eventSource, string logName) { bool removeResult = false; try { if (!ceExists(eventSource)) { removeResult = false; ErrorInfo = "日志分类不存在!"; } else { te(logName); removeResult = true; } } catch (Exception ex) { removeResult = false; ErrorInfo = age; } return removeResult; } ///

/// 获取错误信息 ///

///

public static string GetErrorMessage() { return ErrorInfo; } }}

希望本文所述对大家的C#程序设计有所帮助。