在不同的.NET框架之间共享记录器 [英] Sharing logger between different .NET frameworks

查看:76
本文介绍了在不同的.NET框架之间共享记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可以在.Net Core 1.2,.Net Core 2.0和.NET Framework 4.6+之间共享的应用程序框架。因此,我将项目的目标框架选择为.NET Standard。在我的框架项目中,我具有用于发送文本消息或电子邮件的工厂类。在应用程序启动时,每个应用程序都将带有支持服务的工厂配置为带有DI框架的单例实例。

I am creating an application framework that can be shared between .Net Core 1.2, .Net Core 2.0 and .NET Framework 4.6+. So I choose the target framework of my project as .NET Standard. In my framework project I have factory class that is used to send a text message or email. On application startup, each application configures the factory with the supported services as singleton instance with DI framework.

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。

Issue: the publish method could fail for any reason. And in such case I don't want application to stop, instead the framework should log the error message. My problem is since the framework can be shared between different .NET frameworks, I don't know which type of ILooger I should be using that can be shared between .NET Core and .NET Full.

我试图避免创建自己的 ILogger 接口。同样,当前所有应用程序都使用 Serilog ,但将来可能会改变。

I am trying to avoid creating my own ILogger interface. Also currently all applications are using Serilog but in future that could change.

可以 Microsoft .Extensions.Logging 可以在这里使用?

推荐答案

可以。如果必须支持netcore 1,则

Yes it can. If you have to support netcore 1, then

Install-Package Microsoft.Extensions.Logging -Version 1.1.2 

将工作:

  • https://www.nuget.org/packages/Microsoft.Extensions.Logging/1.1.2 tells that it requires netstandard1.1
  • https://github.com/dotnet/standard/blob/master/docs/versions.md tells you that all of your three target platforms implement netstandard1.1

但更好的是 只需要依靠 Abstractions

Install-Package Microsoft.Extensions.Logging.Abstractions -Version 1.1.2 

您的组件仅需要引用 ILogger ,并且使用您的组件的应用程序不依赖于使用MS扩展记录程序。特别地,如果您查看

Your component only need reference ILogger, and the applications that use your component are not tied to using the MS extensions logger. In particular, if you look at the dependencies for

Serilog.Extensions.Logging -Version 2.0.2 

https://www.nuget.org/packages/Serilog.Extensions.Logging/ ,您可以看到它取决于 Abstractions ,但是不依赖于MS Extensions记录器。
Serilog确实取决于netstandard1.3。但是,netstandard版本页面再次告诉您所有目标都支持它。

At https://www.nuget.org/packages/Serilog.Extensions.Logging/ , you can see that it depends on Abstractions but doesn't depend on the MS Extensions logger. Serilog does depend on netstandard1.3. But again the netstandard version page tells you all your targets support it.

使用框架的应用程序随后可以继续使用Serilog,只要他们知道如何包装serilogger作为MS.Extensions ILogger。 Serilog.Extensions.Logging中的 SerilogLoggerProvider 可以解决问题:

The applications using your framework can then carry on using Serilog, so long as they know how to wrap a serilogger as a MS.Extensions ILogger. the SerilogLoggerProvider in Serilog.Extensions.Logging does the trick:

var msFrameworkLogger= new SerilogLoggerProvider(myExistingSerilog).CreateLogger("name");

这篇关于在不同的.NET框架之间共享记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆