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

30,562

Solution 1

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.

Solution 2

@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 Microsoft.Extensions.Logging, Serilog.Extensions.Logging and Serilog.Sinks.File NuGet packages.

Share:
30,562

Related videos on Youtube

Raghu
Author by

Raghu

Updated on July 09, 2022

Comments

  • Raghu
    Raghu almost 2 years

    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?

    • Developer Dude
      Developer Dude about 3 years
      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.
  • Gabriel Piffaretti
    Gabriel Piffaretti almost 6 years
    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 project
  • Morten Nørgaard
    Morten Nørgaard almost 6 years
    @GabrielPiffaretti The LoggerConfiguration class is in the Serilog-namespace - did you include this Nuget-package? nuget.org/packages/Serilog/2.7.1.
  • Dave Jellison
    Dave Jellison over 5 years
    debiggung? :) :)
  • Morten Nørgaard
    Morten Nørgaard over 5 years
    @DaveJellison Deliberate, I swear :)
  • Dave Jellison
    Dave Jellison over 5 years
    That's why I was smiling :) :)
  • Killnine
    Killnine over 5 years
    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.
  • Daz
    Daz over 4 years
    CreateLogger<T> is an extension method. If you don't import Microsoft.Extensions.Logging into your class file you'll only see CreateLogger(string).
  • Ryan Shripat
    Ryan Shripat over 3 years
    To read configuration from web.config/app.config, import Serilog.Settings.AppSettings.