我的应用程序有一个带有两个提供程序的日志工厂:其中一个提供程序写入文件,另一个提供程序进行REST调用以保存在供应商存储库中。我使用loggerFactory在几乎每个类类上创建一个记录器,其中包括我用于所有rest调用的Rest客户端工厂。
//Creates Rest Clients
public class RestClientFactory
{
public RestClient Create(string name)
{
return new RestClient(this.customLoggerFactory.Create(name));
}
}
//Custom Rest client
public class RestClient{
private readonly ICustomLoggerFactory logger;
public RestClient(ICustomLoggerFactory logger)
{
this.logger = logger;
}
public Response Execute(Request req)
{
try
{
logger.Debug("Start")
// Web Client logic
}
catch(Exception ex)
{
// This is where the problem is. Assuming the Vendor Rest
// Client is failing, the code will loop into this section
logger.Error("Something went wrong", ex);
}
}
}
public class CustomLoggerFactory:ICustomLoggerFactory {
private ILoggerFactory msLogger; //MS framework logger factory
public CustomLoggerFactory(ILoggerFactory msLogger){
this.mslogger = mslogger;
}
CustomLogger Create(string name){
this.loggerFactory.CreateLogger(name);
}
}
public class CustomLogger{
public void Debug(string message){
// Debug logging logic
}
public void Error(string message, Exception ex){
// Error logging logic
}
}
这会创建一个循环,如果出现错误并且供应商站点已关闭,则会发生这种情况:
是否可以在两者之间切换提供者?会说if(restClient是VendorRestClient),而忽略Rest Api日志提供程序吗?我正在尝试确保提供程序的任何错误都不会再次调用同一提供程序。