Skip to content

How to add and configure logging

It is strongly suggested to use dependency injection to setup logging as described in .NET documentation. .NET documentation also contains details about all possible configuration options for logging service.

There are 2 preferred ways how to configure logging. First way is to write settings to application.json file. Here is an example of this approach using Console log provider:

var configuration = new ConfigurationBuilder()
  .AddJsonFile("appsettings.json")
  .Build();
var collection = new ServiceCollection();
collection.AddPlatformClients(o =>
{
  o.ConfigureOpenApiKey(openapikey);
  o.PlatformEnvironment = PlatformEnvironment.Dev0;
});
collection.AddLogging(builder =>
{
  builder
    .AddConfiguration(configuration.GetSection("Logging"))
    .AddConsole();
});

and example of logging section in appsettings.json:

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }

Second way is to configure logging directly in code. Following example is also using Console log provider:

var collection = new ServiceCollection();
collection.AddPlatformClients(o =>
{
    o.ConfigureOpenApiKey(openapikey);
    o.PlatformEnvironment = PlatformEnvironment.Dev0;
});
collection.AddLogging(builder =>
{
    builder
        .SetMinimumLevel(LogLevel.Debug)
        .AddFilter("Default", LogLevel.Debug)
        .AddConsole();
});