SoFunction
Updated on 2025-03-06

C# method to implement custom windows system logs

C# method to implement custom windows system logs

Updated: August 22, 2015 16:49:16 Author: My Heart Still
This article mainly introduces C# to implement custom Windows system logs, involving C#’s techniques for creating, reading, writing and deleting Windows system logs. It is very practical. Friends who need it can refer to it.

This article describes the method of implementing custom Windows system logs in C#. Share it for your reference. The specific implementation method is as follows:

using System;
using ;
using ;
using ;
using ;
namespace ConsoleApp
{
 /// <summary>
 /// System log /// </summary>
 public class PackSystemEventLog
 {
  /// <summary>
  /// error message  /// </summary>
  private static string ErrorInfo { get; set; }
  /// <summary>
  /// Create system event log classification  /// </summary>
  /// <param name="eventSourceName">Register event source (for example, this log comes from a certain application)</param>  /// <param name="logName">Login name (name displayed by event list)</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public static bool CreateSystemEventLogCategory(string eventSourceName, string logName)
  {
   bool createResult = false;
   try
   {
    if (!(eventSourceName))
    {
     (eventSourceName, logName);
    }
    createResult = true;
   }
   catch (Exception ex)
   {
    createResult = false;
    ErrorInfo = ;
   }
   return createResult;
  }
  /// &lt;summary&gt;
  /// Delete the system event log classification  /// &lt;/summary&gt;
  /// <param name="eventSource">EventName Event Source</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public static bool RemoveSystemEventSourceCategory(string eventSource)
  {
   bool createResult = false;
   try
   {
    if ((eventSource))
    {
     (eventSource, ".");
    }
    createResult = true;
   }
   catch (Exception ex)
   {
    createResult = false;
    ErrorInfo = ;
   }
   return createResult;
  }
  /// &lt;summary&gt;
  /// Write logs to system logs  /// &lt;/summary&gt;
  /// <param name="eventSource">Event Source</param>  /// <param name="msg">Write log information</param>  /// <param name="type">Log text classification (warning, information, error)</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public static bool WriteSystemEventLog(string eventSource, string msg, EventLogEntryType type)
  {
   bool writeResult = false;
   try
   {
    if (!(eventSource))
    {
     writeResult = false;
     ErrorInfo = "Log classification does not exist!";     
    }
    else
    {
     (eventSource, msg, type);
     writeResult = true;
    }
   }
   catch (Exception ex)
   {
    writeResult = false;
    ErrorInfo = ;
   }
   return writeResult;
  }
  /// &lt;summary&gt;
  /// Delete the logName in the event source (it seems to have deleted all logs of this category)  /// &lt;/summary&gt;
  /// &lt;param name="eventSource"&gt;&lt;/param&gt;
  /// &lt;param name="logName"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static bool RemoveSystemEventLog(string eventSource, string logName)
  {
   bool removeResult = false;
   try
   {
    if (!(eventSource))
    {
     removeResult = false;
     ErrorInfo = "Log classification does not exist!";
    }
    else
    {
     (logName);
     removeResult = true;
    }
   }
   catch (Exception ex)
   {
    removeResult = false;
    ErrorInfo = ;
   }
   return removeResult;
  }
  /// &lt;summary&gt;
  /// Get error message  /// &lt;/summary&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static string GetErrorMessage()
  {
   return ErrorInfo;
  }
 }
}

I hope this article will be helpful to everyone's C# programming.

  • C#
  • Customize
  • windows
  • log

Related Articles

  • Convert C# string string, memory stream MemoryStream and bit array byte[]

    This article mainly introduces the method of converting string string, memory stream MemoryStream and bit array byte[]. If you need it, please refer to it.
    2016-05-05
  • A brief discussion on the difference between C# non-mode form show() and mode form showdialog()

    Below, the editor will give you a brief article on the difference between C# non-mode form show() and mode form showdialog(). The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with the editor
    2016-07-07
  • Summary of C# TreeView control usage skills

    This article mainly introduces a summary of the skills of using C# TreeView controls. TreeView controls are also used frequently in form applications. When we use TreeView, we generally display resources in a layered manner, similar to the concave representation of trees in data structures.
    2022-08-08
  • Summary of several methods of executing Javascript code in C#

    This article mainly summarizes and introduces several methods of executing Javascript code in C#. Friends who need it can come and refer to it. I hope it will be helpful to you.
    2014-01-01
  • C# thread queue usage example analysis

    This article mainly introduces the usage of C# thread queues, and analyzes the operation techniques of C# thread queues in an example form, which has certain reference value. Friends who need it can refer to it.
    2015-09-09
  • C# commissioned detailed explanation and example sharing

    This article discusses the delegation in C# in detail, lists its main implementation methods, and analyzes its benefits at the design and coding levels. Finally, it will discuss its security and execution efficiency, etc., and of course there are implementation examples.
    2014-03-03
  • MVVM simplified Messager class instance code

    This article mainly introduces relevant materials about the simplified Messager class of MVVM. The article introduces the sample code in detail, which has certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.
    2018-06-06
  • C# Example code for obtaining computer hardware information

    This article mainly introduces sample code for obtaining computer hardware information in C# to help everyone better understand and learn C#. Interested friends can learn about it.
    2020-10-10
  • Detailed explanation of Unity's implementation of batch Build packaging

    Generally speaking, if the project is PC or Android or IOS, there will be no need for batch Build packaging, but if the project is WebGL, you may encounter such needs. This article mainly introduces how to implement Build batch packaging in Unity. Friends who need it can refer to it.
    2021-12-12
  • C# Implementation method for determining whether time periods intersect

    This article mainly introduces the relevant information on the implementation method of C# to determine whether time periods intersect. I hope this article can help you and enable you to implement such functions. Friends who need it can refer to it.
    2017-10-10

Latest Comments