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

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

问题描述

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

公共类 MyFactory : IMyFactory{私人 ILogger _logger;private IDictionary_container = new Dictionary();//在应用程序启动时调用此方法来配置支持的服务公共无效配置(字符串 [] 键,ILogger 记录器){foreach(var 键中的键){如果(键==文本"){container.Add(key,new TextMessageService());}if(key == "email"){container.Add(key,new EmailService());}}}//应用调用此方法发送消息public bool SendMessage(字符串键,字符串消息){//我们不希望应用程序在发布失败时停止,所以我们添加了 try-catch 并记录消息无功标志=假;尝试{var service=容器[key];服务.发布(消息);标志 = 真;}类(例外前){_logger.Error(ex);}返回标志;}}

问题:发布方法可能因任何原因失败.在这种情况下,我不希望应用程序停止,而是框架应该记录错误消息.我的问题是由于该框架可以在不同的 .NET 框架之间共享,我不知道我应该使用哪种类型的 ILooger 可以在 .NET Core 和 .NET Full 之间共享.>

我试图避免创建自己的 ILogger 界面.此外,目前所有应用程序都在使用 Serilog,但未来可能会发生变化.

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

解决方案

是的,可以.如果你必须支持netcore 1,那么

Install-Package Microsoft.Extensions.Logging -Version 1.1.2

会起作用:

但更好的是你的可重用组件只需要依赖于Abstractions:

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

您的组件只需要引用 ILogger,并且使用您的组件的应用程序与使用 MS 扩展记录器无关.特别是,如果您查看

的依赖项

Serilog.Extensions.Logging -Version 2.0.2

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

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

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

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;
    }
}

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.

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

Can Microsoft.Extensions.Logging be used here?

解决方案

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

Install-Package Microsoft.Extensions.Logging -Version 1.1.2 

would work:

But better still your reusable component only need rely on the Abstractions:

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

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 

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.

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天全站免登陆