You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Agent向Server发送Windows事件日志失败求助

问题描述

系统统计数据可正常发送至服务器,但Windows事件日志无法保存到数据库。客户端显示发送日志数量正确,服务端日志也提示已接收事件日志,但数据并未写入数据库。已开启两端日志排查仍未找到原因。

相关代码

服务端代码

public async Task UpdateLogs(List<EventLogEntryModel> eventLogs)
{
   _logger.LogInformation("Received Event Logs");

    try
    {
        foreach (var log in eventLogs)
        {
           // _logger.LogInformation($"Event ID: {log.EventID}, Entry Type: {log.EntryType}, Message: {log.Message}");
            _context.EventLogs.Add(log);

        }
    
      }

    catch (Exception ex) // Catching exceptions to log errors
    {
        _logger.LogError(ex, "An error occurred while updating stats."); // Logging the error
        throw; 
    }
}

客户端代码

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    await ConnectWithRetryAsync(stoppingToken);

    while (!stoppingToken.IsCancellationRequested)
    {
        try
        {
           List<EventLogEntryModel> eventLogs = CollectEventLogs("Application");
            var (cpuUsage, totalRam, diskSpace) = CollectSystemStats();

            if (DateTime.Now.Minute % 1 == 0)
            {
                _logger.LogInformation($"Sending Information: CPU {cpuUsage}, Ram {totalRam}, Disk {diskSpace}");

                var systemStats = new SystemStats
                {
                    CpuUsage = cpuUsage,
                    RamUsage = totalRam,
                    DiskSpace = diskSpace
                };

                await _connection.InvokeAsync("UpdateStats", systemStats);

                _logger.LogInformation($"Sending {eventLogs.Count} logs to the server.");

                await _connection.InvokeAsync("UpdateLogs", eventLogs);
            }
            await Task.Delay(TimeSpan.FromMinutes(3), stoppingToken);

        }
        catch (HubException ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
private List<EventLogEntryModel> CollectEventLogs(string logName)
{
    List<EventLogEntryModel> eventLogsList = new List<EventLogEntryModel>();
 
try
    {
        EventLog eventLog = new EventLog(logName);
        int count = 0;
        foreach (EventLogEntry entry in eventLog.Entries)
        {
            if (count >= 10)
                break;

            eventLogsList.Add(new EventLogEntryModel
            {
                EntryType = entry.EntryType.ToString(),
                EventID = entry.EventID,
                Message = entry.Message
            });
            count++;
        }
    }
    catch (Exception ex)
    {
        _logger.LogError($"An error occurred while collecting event logs: {ex.Message}");
    }

    return eventLogsList;
}
排查修复建议
  • 核心问题:未提交数据库更改
    服务端UpdateLogs方法仅将日志对象添加到EF Core上下文,但没有调用await _context.SaveChangesAsync()提交更改到数据库。对比系统统计的UpdateStats方法,该方法应该包含了提交操作,因此统计数据能正常写入。修改服务端代码:
    public async Task UpdateLogs(List<EventLogEntryModel> eventLogs)
    {
       _logger.LogInformation("Received Event Logs");
    
        try
        {
            foreach (var log in eventLogs)
            {
                _logger.LogInformation($"Event ID: {log.EventID}, Entry Type: {log.EntryType}, Message: {log.Message}");
                _context.EventLogs.Add(log);
            }
            // 添加这行代码提交更改
            await _context.SaveChangesAsync();
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "An error occurred while updating event logs."); // 修改日志提示,避免混淆
            throw; 
        }
    }
    
  • 完善日志排查
    取消服务端单条日志的注释,确认每个日志对象的字段值是否符合数据库表结构要求,排查是否存在字段类型不匹配、必填项缺失等问题。
  • 修正异常日志信息
    将服务端异常日志中的"updating stats"改为"updating event logs",避免排查时混淆业务场景。
  • 排查重复数据冲突
    客户端每次收集前10条事件日志,可能重复发送已存在的日志,导致数据库因主键或唯一约束冲突无法写入。需确认数据库表是否有重复约束,或修改客户端逻辑,只收集上次发送后新增的日志。

内容的提问来源于stack exchange,提问作者Aarin Bailey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 09:40:59