|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
using System.Diagnostics.Tracing;
|
|
|
|
|
|
namespace LogStudy.EventLog
|
|
|
{
|
|
|
class Program
|
|
|
{
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
Console.WriteLine("======== 事件日志学习 ========");
|
|
|
|
|
|
MinimalEventSource minimalEventSource = MinimalEventSource.Log;
|
|
|
minimalEventSource.Write("MiniEventSource", "--------------------------------");
|
|
|
|
|
|
|
|
|
var evtSource = new EventSource("AndyEvent");
|
|
|
evtSource.Write("AndyEvent", "sdfasdfsadfasdfasdfasdf");
|
|
|
|
|
|
|
|
|
DatabaseSource.Instance.OnCommandExecute("我是自定义事件日志!");
|
|
|
|
|
|
WriteWindowsOSLog();
|
|
|
|
|
|
Console.WriteLine("输入回车键,退出程序。");
|
|
|
Console.ReadLine();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 写入Windows 系统日志
|
|
|
/// 需要引入 System.Diagnostics.EventLog 包
|
|
|
/// (需要管理员权限运行)
|
|
|
/// </summary>
|
|
|
/// <remarks>
|
|
|
/// 查看:Windows操作系统的 "事件查看器" --> 应用程序和服务日志 --> AndyLog:右侧日志记录列表
|
|
|
/// </remarks>
|
|
|
static void WriteWindowsOSLog()
|
|
|
{
|
|
|
//只有Windows系统,才可以。
|
|
|
if (OperatingSystem.IsWindows())
|
|
|
{
|
|
|
var sourceName = "Andy-Windows-SystemLog-Source";
|
|
|
var logName = "AndyLog";
|
|
|
|
|
|
if (!System.Diagnostics.EventLog.SourceExists(sourceName))
|
|
|
{
|
|
|
System.Diagnostics.EventLog.CreateEventSource(sourceName, logName);
|
|
|
}
|
|
|
|
|
|
// Create an EventLog instance and assign its source.
|
|
|
System.Diagnostics.EventLog myLog = new System.Diagnostics.EventLog();
|
|
|
myLog.Source = sourceName;
|
|
|
|
|
|
// Write an informational entry to the event log.
|
|
|
myLog.WriteEntry($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}我是执行 .net 程序写入的 Windows 操作系统事件日志!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("非Windows系统,不写入操作系统事件日志!");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
[EventSource(Name = "Samples-EventSourceDemos-EventLog")]
|
|
|
public sealed class MinimalEventSource : EventSource
|
|
|
{
|
|
|
private MinimalEventSource() { }
|
|
|
|
|
|
public static MinimalEventSource Log = new MinimalEventSource();
|
|
|
|
|
|
[Event(1, Message = "{0} -> {1}", Channel = EventChannel.Admin)]
|
|
|
public void Load(long baseAddress, string imageName)
|
|
|
{
|
|
|
WriteEvent(1, baseAddress, imageName);
|
|
|
}
|
|
|
}
|
|
|
}
|