Sharing logger between different .NET frameworks

后端 未结 1 614
时光取名叫无心
时光取名叫无心 2021-01-01 00:06

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 .NE

相关标签:
1条回答
  • 2021-01-01 00:44

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

    Install-Package Microsoft.Extensions.Logging -Version 1.1.2 
    

    would work:

    • 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

    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");
    
    0 讨论(0)
提交回复
热议问题