Skip to content

Latest commit

 

History

History
57 lines (42 loc) · 1.43 KB

dotnet-logging.md

File metadata and controls

57 lines (42 loc) · 1.43 KB

Logging in .NET Core & .NET 5

Quick links

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