.NET Logging

Tips

  • For a netstandard library

    • Add Microsoft.Extensions.Logging to the project (do not add a strong dependency to a logging framework such as log4net or NLog!).

      <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
          <TargetFramework>netcoreapp2.0</TargetFramework>
        </PropertyGroup>
        <ItemGroup>
          <PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
        </ItemGroup>
        <!-- ... -->
      </Project>
    • Add ILogger dependency (IoC) to the class constructor and the related private field

      using Microsoft.Extensions.Logging;
      // ...
      
      public class MyClass
      {
          private readonly ILogger _logger;
      
          public MyClass(ILogger<PuppetFacterService> logger)
          {
              _logger = logger;
          }
      
          public MyMethod()
          {
              _logger.LogInformation("MyMethod called");
          }
      }

Frameworks

Serilog

Homepage: serilog.net

Usecases

Logging in Redis

  • Possible with Serilog

  • NLog.Redis not yet available for .NET Core (as of the 10th of May 2018).

Last updated