How does Microsoft.Extensions.Logging work for full .net framework?
Asked Answered
B

2

38

The article (Monitor and diagnose Azure Service Fabric applications) indicates following (please note text in bold):

ASP.NET Core logging

Choosing how to instrument your code can be difficult, if you chose poorly and have to reinstrument, you are revisiting and potentially destabilizing your code base. To reduce the risk, developers can choose an instrumentation library such as Microsoft.Extensions.Logging provided by ASP.NET Core. This provides an ILogger interface that allows the provider of your choice to be used while minimizing the impact to existing code. Another nice aspect of this is that the code can be used not only in .NET Core on Windows and Linux, but in the full .NET framework too, giving the ability to standardize your instrumentation code across .NET and .NET Core.

How is this supposed to work because when I tried to add the extensions library (to my service fabric cluster application project that compiles to .NET framework 4.5.2), it is attempting to bring down all ASP.NET Core related binaries?

Brantbrantford answered 21/2, 2017 at 0:53 Comment(1)
The recommended solution does not work - at least with the latest package versions. Sorry for posting this as an answer, I do not have enough "cred" to add a comment. Feel free to delete this answer, but be advised that this solution is not working, and developers are suffering from a lack of documentation, understanding and help from Microsoft on how logging works.Virginiavirginie
Q
17

This means that the library 'Microsoft.Extensions.Logging' is compiled against netstandard (1.1), which means it can be used by both full framework (4.5+) applications and dotnet core applications.

Adding the net standard metapackage introduces a bunch of dependencies, but since your project is targeting the full framework, they won't actually be used by your service.

Quillan answered 21/2, 2017 at 18:0 Comment(1)
Since 2.0 of Microsoft.Extensions.Logging, it's built on Netstandard 2.0, which means only Framework 4.6.1 and above is supported. If you can't update to that, and must stay on 4.5.2, then you're stuck with 1.x versions of the Logging packages.Motorbus
S
47

@LoekD's answer is absolutely correct. Here's a .NET Framework example of how to use the Microsoft Extentions Logging framework with Serilog.

public class Program
{
    private static void Main()
    {
        // instantiate and configure logging. Using serilog here, to log to console and a text-file.
        var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory();
        var loggerConfig = new LoggerConfiguration()
            .WriteTo.Console()
            .WriteTo.File("logs\\myapp.txt", rollingInterval: RollingInterval.Day)
            .CreateLogger();
        loggerFactory.AddSerilog(loggerConfig);

        // create logger and put it to work.
        var logProvider = loggerFactory.CreateLogger<Program>();
        logProvider.LogDebug("debiggung");

    }
}

Requires the following NuGet packages

  • Microsoft.Extensions.Logging
  • Serilog.Extensions.Logging
  • Serilog.Sinks.File
Shoestring answered 13/3, 2018 at 11:43 Comment(10)
Included the Nuget Packages you mentioned and can't make it work, it doesn't seem to find LoggerConfiguration class. I'm trying to use it on a .NET Framework 4.6.1 projectBleach
@GabrielPiffaretti The LoggerConfiguration class is in the Serilog-namespace - did you include this Nuget-package? nuget.org/packages/Serilog/2.7.1.Griffon
debiggung? :) :)Spoilfive
@DaveJellison Deliberate, I swear :)Griffon
That's why I was smiling :) :)Spoilfive
CreateLogger<T> is an extension method. If you don't import Microsoft.Extensions.Logging into your class file you'll only see CreateLogger(string).Fitzger
To read configuration from web.config/app.config, import Serilog.Settings.AppSettings.Cordellcorder
Hello, @MortenNørgaard, can I ask, if I were to want to use log4net instead of serilog in the above example, what additional steps do i need to follow? Do I just need to find the log4net equivalent of loggerFactory.AddSerilog() ie (loggerFactory.AddLog4net())?Gamophyllous
@TanYuHauSean Yes. A friendly developer has - at the time of this writing - made a .NET 5 compatible extension at github.com/huorswords/… that may be of use to you.Griffon
yup! i was a bit confused at first since it said it is compatible for ASP.NET, but it still works for console applications just fineGamophyllous
Q
17

This means that the library 'Microsoft.Extensions.Logging' is compiled against netstandard (1.1), which means it can be used by both full framework (4.5+) applications and dotnet core applications.

Adding the net standard metapackage introduces a bunch of dependencies, but since your project is targeting the full framework, they won't actually be used by your service.

Quillan answered 21/2, 2017 at 18:0 Comment(1)
Since 2.0 of Microsoft.Extensions.Logging, it's built on Netstandard 2.0, which means only Framework 4.6.1 and above is supported. If you can't update to that, and must stay on 4.5.2, then you're stuck with 1.x versions of the Logging packages.Motorbus

© 2022 - 2024 — McMap. All rights reserved.