Search code examples
nlog

NLog, custom LogLevels


In the config file config.json I have this entry

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

I'm lacking the terminology to do a proper search here.

I've added an extra entry, "MyEntry". What is that? Is it a category? What would I need to do to get it to do something?

What I'm actually trying to do is just make a reference sandbox project so that I can play with the various settings of nlog


Solution

  • Sorted now thanks to the help in the comments

    Here's 2 examples, one using a class name, and one using a random name.

    The log file

        "LogLevel": {
            "Default": "Critical",
            "Microsoft": "Critical",
            "Microsoft.Hosting.Lifetime": "Critical",
            "NLogUsing.Controllers.TempController": "Critical",
            "SomeRandomNameLogger": "Information"
        }
      },
    

    And some code that uses it

    namespace NLogUsing.Controllers
    {
       [Route("/Temp")]
        public class TempController : Controller
        {
            private readonly ILogger<TempController> classLogger;
            private readonly Microsoft.Extensions.Logging.ILogger namedLogger = null;
    
            public TempController(ILogger<TempController> logger, ILoggerFactory logFactory)
            {
                classLogger = logger;
                namedLogger = logFactory.CreateLogger("SomeRandomNameLogger");
            }
    
            [AllowAnonymous]
            [HttpGet("Bob")]
            public async Task<string> Bob()
            {
                classLogger.LogDebug(1, "ClassName Debug");
                classLogger.LogInformation(1, "ClassName Info");
                classLogger.LogTrace(1, "ClassName trace");
                classLogger.LogWarning(1, "ClassName warning");
                classLogger.LogCritical(1, "ClassName critical");
                classLogger.LogError(1, "ClassName error");
    
                namedLogger.LogDebug(1, "Named Debug");
                namedLogger.LogInformation(1, "Named Info");
                namedLogger.LogTrace(1, "Named trace");
                namedLogger.LogWarning(1, "Named warning");
                namedLogger.LogCritical(1, "Named critical");
                namedLogger.LogError(1, "Named error");
    
    
                return await Task.FromResult<string>("Bob");
            }