'Logging in .NET Core without DI?
It seems that Microsoft are really trying to shove DI down your throat with .NET Core, and I'm not sure why, but frankly my console app is small and simple and I just don't want to build a whole DI container just to do some simple logging. How can I do logging in .NET Core without using DI? Everything I've read assumed you're going to use .NET Core's built-in logging architecture which obviously requires DI, but there must be a way to just do it without DI using a static variable on the class?
Solution 1:[1]
If you want to do it yourself you will need to instantiate a LoggerFactory instance somewhere and configure what providers you want. Then you just need to call CreateLogger to create a instance or use new Logger<T>(ILoggerFactory) to create a logger.
using Microsoft.Extensions.Logging;
static class MyLogger {
public static ILoggerFactory LoggerFactory {get;}
static MyLogger() {
LoggerFactory = new LoggerFactory();
LoggerFactory.AddConsole();
}
}
public MyClass {
private readonly ILogger _logger = new Logger<MyClass>(MyLogger.LoggerFactory);
}
Solution 2:[2]
As per Ilyas and bokibegs comments in Scotts answer here is the currently working code for .NET 5.0
using Microsoft.Extensions.Logging
var factory = LoggerFactory.Create(b => b.AddConsole());
var logger = factory.CreateLogger<T>();
This requires the Microsoft.Extensions.Logging and Microsoft.Extensions.Logging.Console nuget packages.
Solution 3:[3]
Complete example using .NET Core 6 and NLog 4.7.
- Run
dotnet new console - Run
dotnet add package NLog - Create a new text file named
NLog.config1.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="file.txt" />
<target name="logconsole" xsi:type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
- Add the
NLog.configfile to your project file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Include="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.7.13" />
</ItemGroup>
</Project>
- Edit the Program.cs file as follows:
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Info("Program started.");
logger.Info("Program completed.");
- Run
dotnet run.
Expected Output
> dotnet run
2022-02-08 17:23:37.0430|INFO|Program|Program started.
2022-02-08 17:23:37.0924|INFO|Program|Program completed.
1https://github.com/NLog/NLog/wiki/Tutorial#configure-nlog-targets-for-output
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 | |
| Solution 2 | Eonasdan |
| Solution 3 | Wallace Kelly |
