c# - 有没有一种方法可以跨不同语言标准化 Windows 事件日志查询?

标签 c# windows standards event-log non-english

我有一些 C# 代码似乎可以很好地监视 Windows 事件 4624,并计划在 Windows 服务中使用它,以便在用户成功登录系统时提供通知。然而,该代码可能会在非英语计算机上使用,我假设我的代码在它们上无法达到预期的结果。我做了一些搜索,但在我看来,没有一种简单的方法可以在多种语言上监视此事件。 Here is on example ,和here is another one我发现了。

也许最简单的解决方案是为不同的语言系统提供不同版本的程序,或者内置逻辑来检测语言并采取相应的行动,但这两种选择对我来说似乎都很笨拙。

是否有一些标准化的方法来收集我缺少的这些信息?

public static async Task subscribe4624()
        {
            await Task.Run(() =>
            {
                EventLogWatcher watcher4624 = null;
                try
                {
                    EventLogQuery subscriptionQuery4624 = new EventLogQuery(
                        "Security", PathType.LogName, "*[System/EventID=4624]");

                    watcher4624 = new EventLogWatcher(subscriptionQuery4624);

                    // Make the watcher listen to the EventRecordWritten
                    // events.  When this event happens, the callback method
                    // (EventLogEventRead) is called.
                    watcher4624.EventRecordWritten +=
                        new EventHandler<EventRecordWrittenEventArgs>(
                            EventLogEventRead4624);

                    // Activate the subscription
                    watcher4624.Enabled = true;

                    for (; ; )
                    {
                        // Wait for events to occur. 
                        System.Threading.Thread.Sleep(1000);
                    }


                }
                catch (EventLogReadingException e)
                {
                    log.Info("Error reading the log: {0}" + e.Message);
                }
                finally
                {
                    // Stop listening to events
                    watcher4624.Enabled = false;

                    if (watcher4624 != null)
                    {
                        watcher4624.Dispose();
                    }
                }
            });
        }
// Callback method that gets executed when an event is
        // reported to the subscription.
        public static void EventLogEventRead4624(object obj,
        EventRecordWrittenEventArgs arg)
        {
            // Make sure there was no error reading the event.
            if (arg.EventRecord != null)
            {
                //////
                // This section creates a list of XPath reference strings to select
                // the properties that we want to display
                // In this example, we will extract the User, TimeCreated, EventID and EventRecordID
                //////
                // Array of strings containing XPath references
                String[] xPathRefs = new String[5];
                xPathRefs[0] = "Event/System/TimeCreated/@SystemTime";
                xPathRefs[1] = "Event/System/Computer";
                xPathRefs[2] = "Event/EventData/Data[@Name=\"TargetUserName\"]";
                xPathRefs[3] = "Event/EventData/Data[@Name=\"TargetDomain\"]";
                xPathRefs[4] = "Event/EventData/Data[@Name=\"LogonType\"]";
                // Place those strings in an IEnumerable object
                IEnumerable<String> xPathEnum = xPathRefs;
                // Create the property selection context using the XPath reference
                EventLogPropertySelector logPropertyContext = null;
                try
                {
                    logPropertyContext = new EventLogPropertySelector(xPathEnum);
                }
                catch (Exception ex)
                {
                    log.Info(ex.Message);
                }
                IList<object> logEventProps = ((EventLogRecord)arg.EventRecord).GetPropertyValues(logPropertyContext);

                if (logEventProps[4].ToString() == "2")
                {


                    log.Info("Time: " + logEventProps[0]);
                    log.Info("Computer: " + logEventProps[1]);
                    log.Info("TargetUserName: " + logEventProps[2]);
                    log.Info("TargetDomainName: " + logEventProps[3]);
                    log.Info("LogonType: " + logEventProps[4]);
                    log.Info("---------------------------------------");

                    //log.Info("Description: "+ arg.EventRecord.FormatDescription());

                    Console.WriteLine("Time: " + logEventProps[0]);
                    Console.WriteLine("Computer: " + logEventProps[1]);
                    Console.WriteLine("TargetUserName: " + logEventProps[2]);
                    Console.WriteLine("TargetDomainName: " + logEventProps[3]);
                    Console.WriteLine("LogonType: " + logEventProps[4]);
                    Console.WriteLine("---------------------------------------");

                    //Console.WriteLine("Description: " + arg.EventRecord.FormatDescription());
                }
            }
            else
            {
                log.Info("The event instance was null.");
            }
        }

最佳答案

这是来 self 的计算机的消息 4621 的 XML(操作系统语言是德语):

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  <System>
    <Provider Name="Microsoft-Windows-Security-Auditing" Guid="{54849625-5478-4994-a5ba-3e3b0328c30d}" /> 
    <EventID>4624</EventID> 
    <Version>2</Version> 
    <Level>0</Level> 
    <Task>12544</Task> 
    <Opcode>0</Opcode> 
    <Keywords>0x8020000000000000</Keywords> 
    <TimeCreated SystemTime="2022-02-14T12:10:32.8804196Z" /> 
    <EventRecordID>706697</EventRecordID> 
    <Correlation ActivityID="{9d82cb76-0e04-0002-21cc-829d040ed801}" /> 
    <Execution ProcessID="1180" ThreadID="58872" /> 
    <Channel>Security</Channel> 
    <Computer>DESKTOP-Win10</Computer> 
    <Security /> 
  </System>
  <EventData>
    <Data Name="SubjectUserSid">S-1-5-18</Data> 
    <Data Name="SubjectUserName">DESKTOP-WIN10$</Data> 
    <Data Name="SubjectDomainName">WORKGROUP</Data> 
    <Data Name="SubjectLogonId">0x3e7</Data> 
    <Data Name="TargetUserSid">S-1-5-18</Data> 
    <Data Name="TargetUserName">SYSTEM</Data> 
    <Data Name="TargetDomainName">NT-AUTORITÄT</Data> 
    <Data Name="TargetLogonId">0x3e7</Data> 
    <Data Name="LogonType">5</Data> 
    <Data Name="LogonProcessName">Advapi</Data> 
    <Data Name="AuthenticationPackageName">Negotiate</Data> 
    <Data Name="WorkstationName">-</Data> 
    <Data Name="LogonGuid">{00000000-0000-0000-0000-000000000000}</Data> 
    <Data Name="TransmittedServices">-</Data> 
    <Data Name="LmPackageName">-</Data> 
    <Data Name="KeyLength">0</Data> 
    <Data Name="ProcessId">0x480</Data> 
    <Data Name="ProcessName">C:\Windows\System32\services.exe</Data> 
    <Data Name="IpAddress">-</Data> 
    <Data Name="IpPort">-</Data> 
    <Data Name="ImpersonationLevel">%%1833</Data> 
    <Data Name="RestrictedAdminMode">-</Data> 
    <Data Name="TargetOutboundUserName">-</Data> 
    <Data Name="TargetOutboundDomainName">-</Data> 
    <Data Name="VirtualAccount">%%1843</Data> 
    <Data Name="TargetLinkedLogonId">0x0</Data> 
    <Data Name="ElevatedToken">%%1842</Data> 
  </EventData>
</Event>

因此实体名称仍然是英文,但有些值不是英文。

关于c# - 有没有一种方法可以跨不同语言标准化 Windows 事件日志查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71111347/

相关文章:

c# - EPPlus Excel AddPicture 错误 'Column number out of bounds'

c# - 在多个线程中使用单个位图 Graphics.CopyFromScreen 的最有效方法?

c# - 从任务管理器启动进程并隐藏命令行参数

c++ - 如何正确设置 CMakeLists.txt 文件?

html - meta 标签应该是 HTML 编码的吗?

c - 使用方括号和符号获取地址

c# - 使用存在的 EF 查询抛出内存不足异常

c# - 如何让 Helm 机转到某个角度,例如60度

windows - 如何使用 Powershell 搜索 Windows 搜索索引文件

c++ - 0x80000000 的一元减号(有符号和无符号)