'How to configure ILogger in Console App with .Net Framework (not .Net Core)

Is it possible to use an ILogger log in a .Net Framework console app? I searched a lot but I wasn't able to find a sample. This is what I came up with so far

        var loggerFactory = new LoggerFactory();
        //loggerFactory.AddProvider();
        var logger = loggerFactory.CreateLogger("Category");
        logger.LogInformation("This is a log line");

I don't know how to add a LoggerProvider. Are there providers already implemented for Console and File for .Net Framework or they are only for Core?

Do I need to implement my own providers?



Solution 1:[1]

using Microsoft.Extensions.Logging;

namespace Test
{   
    class Program
    {
        // Single-Threaded Apartment required for OAuth2 Authz Code flow (User Authn) to execute for this demo app
        [STAThread]
        static void Main(string[] args)
        {
            var log = new VerboseDiagnosticsTraceWriter();
            log.LogInformation($"test");
        }
    }
}

using Microsoft.Extensions.Logging;
using System;

namespace Test
{
    internal class VerboseDiagnosticsTraceWriter : ILogger
    {


        public IDisposable BeginScope<TState>(TState state)
        {
            return null;
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return true;
        }

        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            //System.Diagnostics.Debug.WriteLine($"{logLevel} {state.ToString()}");
            Console.WriteLine($"[{DateTime.Now}] {logLevel} {state.ToString()}");
        }

        //public override void Trace(Microsoft.Azure.WebJobs.Host.TraceEvent traceEvent)
        //{
        //    System.Diagnostics.Debug.WriteLine(traceEvent.Message);
        //}
    }
}

Solution 2:[2]

If you want to use the current computer record to find a matching CB Number or Serial from the second CSV, you can use a where statement for that.

Here is a trimmed down example demonstrating that.

# CSV #1 - Computer / Action
$CB = import-csv -Path "\\Scripts\GAM test\CBs" 
# CSV #2 - DeviceID / CB Number /Serial
$GAM = import-csv -Path "\\Scripts\GAM\CSV\$yesterday-GAMprintCROSbasic.csv"

foreach ($record in $CB) {
    if ($record.action -eq "Unlock") {
        # Select  matching value 
        $GamRecord = $Gam | Where { $_.'CB Number' -eq $Record.Computer -or $_.'Serial' -eq $Record.Computer } | Select -First 1
        if ($null -eq $GamRecord) {
            Write-Warning "No match for: $($record.Computer)"
            Continue
        }

        $Serial = $GamRecord.Serial    


 
        # $GamRecord.'CB NUmber'
        # $GamRecord.DeviceID

    }

}

Once you have the $GamRecord, which is the first record that correlated with $Record.Computer either in its CB Number or Serial, you can access all the properties from both CSVS through their respective variable.

$record.Computer # Is a match to either $GamRecord.'CB Number' or $GamRecord.Serial
$record.Action
$GamRecord.'CB Number'
$GamRecord.DeviceId
$GamRecord.Serial

Note that I left a if ($null -eq $GamRecord) { as you might want to do something specific in the event Computer from the first CSV do not correspond to any CB Number or Serial in CSV2.

Also note the use of -eq when doing a comparison in the if statements. In your initial sample, as pointed out by one of the commenter, you used if ($record.action = "Unlock"), which is incorrect. = is only used to assign a value to a variable, not as a comparison operator. For the latter, -eq is used.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Javier
Solution 2 Sage Pourpre