0

I have a small console app. I have enabled Debug and Console logging. However, they do not seem to be honouring my config for log level. Here is my setup:

Add Config file and create logger

// global to Program
private static ILogger<Program> _logger;
private const string _configFile = "appsettings.json";


 //inside Main
_config = new ConfigurationBuilder()
    .AddJsonFile(_configFile, optional: false, reloadOnChange: true)
    //.AddEnvironmentVariables()
    .AddCommandLine(args)
    .Build();

using var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddDebug();
    builder.AddSimpleConsole();
});

_logger = loggerFactory.CreateLogger<Program>();

appconfig.json

"Logging": {
    "IncludeScopes": true,
    "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Warning"
    },
    "Debug": {
        "LogLevel": {
            "Default": "Trace",
            "Microsoft": "Warning",
            "FleetLogix.TcaProcessor": "Trace"
        }
    },
    "SimpleConsole": {
        "IncludeScopes": "false",
        "SingleLine": "false",
        "TimestampFormat": "yyyy-MM-dd HH:mm:ss ",
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "FleetLogix.TcaProcessor": "Information"
        }
    }
},

Its currently set to log to two providers, Debug (visual studio run pane) and Console. This is working. Debug is meant to log at Trace, but is only logging at Info. Console is logging at Info, but is not prefixing the timestamp onto the messages.

Is my config setup correctly? (correct property names, correct depth, etc) What should I change it to?

Console also has the awful line break that reading tells me you need to fork Microsoft.Extensions.Logging.Console to fix, so another day. :/

Hecatonchires
  • 1,009
  • 4
  • 13
  • 42
  • You may look at [Logging](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0) and [console log formatter](https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter) docs – Pavel Anikhouski Dec 08 '20 at 12:22

2 Answers2

2

I know you've moved on to Serilog, but the answer here is that you're not connecting the config up to the LoggerFactory. You need to identify the config object and also which section to load logging settings from, using the .AddConfiguration extension method from Microsoft.Extensions.Configuration:

using Microsoft.Extensions.Configuration;

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConfiguration(_config.GetSection("Logging"));
    builder.AddDebug();
    builder.AddSimpleConsole();
});

This question is similar: .NET Non-host LoggerFactory configuration

Julian Melville
  • 185
  • 2
  • 9
0

I ended up switching to Serilog as I needed a file writer anyway. The only hard part was working out the differences between the older and current version, where rollingfile sink has now been integrated into file sink

Program.cs

#region Initialise Logger

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(_config)
    .CreateLogger();

Log.Information("Logger started");

#endregion

appsettings.json

"Serilog": {
    "Using": [
        "Serilog.Sinks.Console",
        "Serilog.Sinks.File" 
    ],
    "MinimumLevel": "Debug",
    "WriteTo": [
        {
            "Name": "Console" 
        },
        {
            "Name": "File",
            "Args": {
                "path": "c:\\scripts\\log\\myprogram.log",
                "rollingInterval": "Day",
                "retainedFileCountLimit": 7
            }
        }
    ],
    "Properties": {
        "Application": "MyProgram"
    }
},
Hecatonchires
  • 1,009
  • 4
  • 13
  • 42