Skip to content

Commit

Permalink
Update syslogs
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed May 6, 2024
1 parent b68f9f0 commit bf5ff85
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Apisite/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(Global.Config)
.UseNLog()
//.UseNLog()
.UseStartup<Startup>();
}

Expand Down
39 changes: 36 additions & 3 deletions src/AgileConfig.Server.EventHandler/ServiceInfoUpdateHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AgileConfig.Server.Event;
using AgileConfig.Server.IService;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Net;

namespace AgileConfig.Server.EventHandler
Expand All @@ -14,18 +15,22 @@ public class ServiceRegisterHandler : IEventHandler<ServiceRegisteredEvent>
{
private readonly IRemoteServerNodeProxy _remoteServerNodeProxy;
private readonly IServerNodeService _serverNodeService;
private readonly ISysLogService _sysLogService;

public ServiceRegisterHandler(
IRemoteServerNodeProxy remoteServerNodeProxy,
IServerNodeService serverNodeService
IServerNodeService serverNodeService,
ISysLogService sysLogService
)
{
_remoteServerNodeProxy = remoteServerNodeProxy;
_serverNodeService = serverNodeService;
_sysLogService = sysLogService;
}

public async Task Handle(IEvent evt)
{
var evtInstance = evt as ServiceRegisteredEvent;
var serverNodes = await _serverNodeService.GetAllNodesAsync();
foreach (var serverNode in serverNodes.Where(x => x.Status == NodeStatus.Online))
{
Expand All @@ -39,25 +44,36 @@ public async Task Handle(IEvent evt)
};
_ = _remoteServerNodeProxy.AllClientsDoActionAsync(serverNode.Id, act);
}

await _sysLogService.AddSysLogAsync(new SysLog
{
LogTime = DateTime.Now,
LogType = SysLogType.Normal,
LogText = $"服务【{evtInstance.UniqueId}】【注册】成功",

Check warning on line 52 in src/AgileConfig.Server.EventHandler/ServiceInfoUpdateHandlers.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Dereference of a possibly null reference.

Check warning on line 52 in src/AgileConfig.Server.EventHandler/ServiceInfoUpdateHandlers.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Dereference of a possibly null reference.
});
}
}

public class ServiceUnRegisterHandler : IEventHandler<ServiceUnRegisterEvent>
{
private readonly IRemoteServerNodeProxy _remoteServerNodeProxy;
private readonly IServerNodeService _serverNodeService;
private readonly ISysLogService _sysLogService;

public ServiceUnRegisterHandler(
IRemoteServerNodeProxy remoteServerNodeProxy,
IServerNodeService serverNodeService
IServerNodeService serverNodeService,
ISysLogService sysLogService
)
{
_remoteServerNodeProxy = remoteServerNodeProxy;
_serverNodeService = serverNodeService;
_sysLogService = sysLogService;
}

public async Task Handle(IEvent evt)
{
var evtInstance = evt as ServiceUnRegisterEvent;
var serverNodes = await _serverNodeService.GetAllNodesAsync();
foreach (var serverNode in serverNodes.Where(x => x.Status == NodeStatus.Online))
{
Expand All @@ -71,6 +87,13 @@ public async Task Handle(IEvent evt)
};
_ = _remoteServerNodeProxy.AllClientsDoActionAsync(serverNode.Id, act);
}

await _sysLogService.AddSysLogAsync(new SysLog
{
LogTime = DateTime.Now,
LogType = SysLogType.Normal,
LogText = $"服务【{evtInstance.UniqueId}】【卸载】成功",

Check warning on line 95 in src/AgileConfig.Server.EventHandler/ServiceInfoUpdateHandlers.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Dereference of a possibly null reference.

Check warning on line 95 in src/AgileConfig.Server.EventHandler/ServiceInfoUpdateHandlers.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Dereference of a possibly null reference.
});
}
}

Expand All @@ -81,19 +104,22 @@ public class ServiceStatusUpdateHandler : IEventHandler<ServiceStatusUpdateEvent
private ILogger _logger;
private readonly IServerNodeService _serverNodeService;
private readonly IServiceInfoService _serviceInfoService;
private readonly ISysLogService _sysLogService;

public ServiceStatusUpdateHandler(
IRemoteServerNodeProxy remoteServerNodeProxy,
ILoggerFactory loggerFactory,
IRestClient restClient,
IServerNodeService serverNodeService,
IServiceInfoService serviceInfoService
IServiceInfoService serviceInfoService,
ISysLogService sysLogService
)
{
_remoteServerNodeProxy = remoteServerNodeProxy;
_restClient = restClient;
_serverNodeService = serverNodeService;
_serviceInfoService = serviceInfoService;
_sysLogService = sysLogService;
_logger = loggerFactory.CreateLogger<ServiceStatusUpdateHandler>();
}

Expand All @@ -120,6 +146,13 @@ public async Task Handle(IEvent evt)
return;
}
var service = await _serviceInfoService.GetByUniqueIdAsync(id);
await _sysLogService.AddSysLogAsync(new SysLog
{
LogTime = DateTime.Now,
LogType = SysLogType.Normal,
LogText = $"服务【{id}】状态更新为【{service.Status}",
});

if (service != null && !string.IsNullOrWhiteSpace(service.AlarmUrl) &&
service.Status == ServiceStatus.Unhealthy)
{
Expand Down

0 comments on commit bf5ff85

Please sign in to comment.