BotSharp/src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs

29 lines
764 B
C#
Raw Normal View History

2024-02-28 16:21:14 +00:00
using BotSharp.Abstraction.Infrastructures;
namespace BotSharp.Core.Infrastructures;
public static class HookEmitter
{
public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, Action<T> action)
{
var logger = services.GetRequiredService<ILogger<T>>();
var result = new HookEmittedResult();
var hooks = services.GetServices<T>();
foreach (var hook in hooks)
{
try
{
2024-09-10 22:12:17 +00:00
logger.LogInformation($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
2024-02-28 16:21:14 +00:00
action(hook);
}
catch (Exception ex)
{
logger.LogError(ex.ToString());
}
}
return result;
}
}