【发布时间】:2018-03-21 16:17:50
【问题描述】:
我正在创建一个可以在 .Net Core 1.2、.Net Core 2.0 和 .NET Framework 4.6+ 之间共享的应用程序框架。所以我选择我项目的目标框架为 .NET Standard。在我的框架项目中,我有用于发送文本消息或电子邮件的工厂类。在应用程序启动时,每个应用程序将具有支持服务的工厂配置为具有 DI 框架的单例实例。
public class MyFactory : IMyFactory
{
private ILogger _logger;
private IDictionary<string,IMyService> _container = new Dictionary<string,IMyService>();
// this method is called on application startup to configure supported services
public void Configure(string[] keys, ILogger logger)
{
foreach(var key in keys)
{
if(key == "text")
{
container.Add(key,new TextMessageService());
}
if(key == "email")
{
container.Add(key,new EmailService());
}
}
}
//application call this method to send message
public bool SendMessage(string key, string message)
{
// we don't want application to stop when publish fail, so we add try-catch and log the message
var flag = false;
try
{
var service= container[key];
service.Publish(message);
flag = true;
}
class(Exception ex)
{
_logger.Error(ex);
}
return flag;
}
}
问题:发布方法可能因任何原因而失败。在这种情况下,我不希望应用程序停止,而是框架应该记录错误消息。我的问题是由于框架可以在不同的 .NET 框架之间共享,我不知道应该使用哪种类型的 ILooger 可以在 .NET Core 和 .NET Full 之间共享。
我试图避免创建自己的ILogger 接口。目前所有应用程序都在使用Serilog,但将来可能会改变。
这里可以使用Microsoft.Extensions.Logging吗?
【问题讨论】:
-
您是否尝试过使用
Microsoft.Extensions.Logging? -
嗯?
ILogger不是某种语言功能。有各种日志框架和门面暴露了ILogger接口。您只需要 that 库可供所有三个目标使用。实际上,几乎所有的日志框架都应该如此。
标签: c# .net .net-core asp.net-core-2.0 coreclr