Reputation: 29983
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?
Upvotes: 41
Views: 28908
Reputation: 1987
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(builder => {
builder.AddConsole();
});
var logger = factory.CreateLogger<T>();
This requires the Microsoft.Extensions.Logging
and Microsoft.Extensions.Logging.Console
nuget packages.
Upvotes: 24
Reputation: 17369
Complete example using .NET Core 6 and NLog 4.7.
dotnet new console
dotnet add package NLog
NLog.config
1.<?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>
NLog.config
file 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>
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Info("Program started.");
logger.Info("Program completed.");
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
Upvotes: 3
Reputation: 29983
What I ended up doing was using NLog but rather than using their .NET Core DI extensions, I just included the main NLog NuGet package, manually creating my own NLog.config
file, and followed the tutorial to GetCurrentClassLogger()
, creating that as a static member of my class, then directly used that for logging. This is quite a simple setup and has no need for DI. NLog should probably document it better.
Upvotes: 8
Reputation: 127543
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);
}
Upvotes: 12