'Logging on .net MAUI
.net MAUI recently removed logging in one of it's newest release. What is the alternative now and how should it be implemented? Have been going all over online, but couldn't find a single example of any logging architecture implemented. Tried log4net, NLog, but did not manage to set any of them up at the end. There is 0 examples online for setting it up any logging on MAUI.
Also, saw the builder.Services.AddLogging() and builder.Logging.Services in MauiProgram which should work with dependency injection, but can't find any Maui example for that implementation too.
How one should set up a basic logging in MAUI now?
Solution 1:[1]
Start by adding a reference to Microsoft.Extensions.Logging.Debug. You could do it like this if you only want it in debug mode:
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
</ItemGroup>
Then when the application starts:
var builder = MauiApp.CreateBuilder();
// ...
#if DEBUG
builder.Services.AddLogging(configure =>
{
configure.AddDebug();
});
#endif
You could also add filters:
#if DEBUG
builder.Services.AddLogging(configure =>
{
configure.AddDebug()
.AddFilter("MyCompany.MyApp.Namespace", LogLevel.Trace)
.AddFilter("Microsoft", LogLevel.Warning);
});
#endif
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 |
