35

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?

one noa
  • 345
  • 1
  • 3
  • 10
Raghu
  • 2,859
  • 4
  • 33
  • 65
  • 2
    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. – Developer Dude Feb 23 '21 at 18:05

2 Answers2

41

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

Morten Nørgaard
  • 2,609
  • 2
  • 24
  • 24
  • 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 – Gabriel Piffaretti Jun 18 '18 at 20:19
  • 1
    @GabrielPiffaretti The LoggerConfiguration class is in the Serilog-namespace - did you include this Nuget-package? https://www.nuget.org/packages/Serilog/2.7.1. – Morten Nørgaard Jun 19 '18 at 05:45
  • 3
    debiggung? :) :) – Dave Jellison Aug 15 '18 at 18:29
  • @DaveJellison Deliberate, I swear :) – Morten Nørgaard Aug 15 '18 at 21:05
  • That's why I was smiling :) :) – Dave Jellison Aug 24 '18 at 02:50
  • 3
    CreateLogger is an extension method. If you don't import Microsoft.Extensions.Logging into your class file you'll only see CreateLogger(string). – Daz Nov 21 '19 at 09:26
  • To read configuration from web.config/app.config, import Serilog.Settings.AppSettings. – Ryan Shripat Dec 02 '20 at 12:01
  • 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()`)? – Tan Yu Hau Sean Jan 03 '23 at 13:26
  • @TanYuHauSean Yes. A friendly developer has - at the time of this writing - made a .NET 5 compatible extension at https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore that may be of use to you. – Morten Nørgaard Jan 04 '23 at 13:38
  • 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 fine – Tan Yu Hau Sean Jan 06 '23 at 12:41
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.

LoekD
  • 11,402
  • 17
  • 27
  • 9
    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. – Killnine Aug 27 '18 at 15:06