.net MAUI 最近在其最新版本之一中删除了日志记录。现在有什么替代方案以及应该如何实施?已经在网上查遍了,但找不到任何实现的日志架构的示例。尝试过
log4net
、NLog
,但最终都没有成功设置。在线有 0 个示例用于设置 MAUI 上的任何日志记录。
此外,在 MauiProgram 中看到了
builder.Services.AddLogging()
和 builder.Logging.Services
,它们应该与依赖注入一起使用,但也找不到该实现的任何 Maui 示例。
现在应该如何在 MAUI 中设置基本日志记录?
首先添加对
Microsoft.Extensions.Logging.Debug
的引用。如果你只想在调试模式下,你可以这样做:
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
</ItemGroup>
然后当应用程序启动时:
var builder = MauiApp.CreateBuilder();
// ...
#if DEBUG
builder.Services.AddLogging(
configure =>
{
configure.AddDebug();
}
);
#endif
您还可以添加过滤器:
#if DEBUG
builder.Services.AddLogging(
configure =>
{
configure
.AddDebug()
.AddFilter("MyCompany.MyApp.Namespace", LogLevel.Trace)
.AddFilter("Microsoft", LogLevel.Warning);
}
);
#endif
如果您想在 Android 上使用本机日志记录支持,一个简单的解决方法是添加对
Microsoft.Extensions.Logging.Console
的引用,然后配置它:
builder.Services.AddLogging(
configure =>
{
configure.AddDebug();
configure.AddConsole();
}
);
虽然这有效,但日志有点难以阅读。更好的方法是使用包装本机日志记录支持的自定义日志记录提供程序。在 Android 文件夹中,添加以下代码:
using Microsoft.Extensions.Logging;
namespace MyMauiApp;
public class AndroidLoggerProvider : ILoggerProvider
{
public AndroidLoggerProvider()
{
}
public ILogger CreateLogger(string categoryName)
{
// Category name is often the full class name, like
// MyApp.ViewModel.MyViewModel
// This removes the namespace:
int lastDotPos = categoryName.LastIndexOf('.');
if (lastDotPos > 0)
{
categoryName = categoryName.Substring(lastDotPos + 1);
}
return new AndroidLogger(categoryName);
}
public void Dispose() { }
}
public class AndroidLogger : ILogger
{
private readonly string Category;
public IDisposable BeginScope<TState>(TState state) => null!;
public bool IsEnabled(LogLevel logLevel) => true;
public AndroidLogger(string category)
{
Category = category;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
string message = formatter(state, exception);
Java.Lang.Throwable? throwable = null;
if (exception is not null)
{
throwable = Java.Lang.Throwable.FromException(exception);
}
switch (logLevel)
{
case LogLevel.Trace:
Android.Util.Log.Verbose(Category, throwable, message);
break;
case LogLevel.Debug:
Android.Util.Log.Debug(Category, throwable, message);
break;
case LogLevel.Information:
Android.Util.Log.Info(Category, throwable, message);
break;
case LogLevel.Warning:
Android.Util.Log.Warn(Category, throwable, message);
break;
case LogLevel.Error:
Android.Util.Log.Error(Category, throwable, message);
break;
case LogLevel.Critical:
Android.Util.Log.Wtf(Category, throwable, message);
break;
}
}
}
然后像这样配置它:
builder.Services.AddLogging(
configure =>
{
// You don't need the debug logger on Android if you use AndroidLoggerProvider.
// configure.AddDebug();
#if ANDROID
#if DEBUG
LogLevel androidLogLevel = LogLevel.Debug;
#else
LogLevel androidLogLevel = LogLevel.Information;
#endif
configure
.AddProvider(new AndroidLoggerProvider())
.AddFilter("MyMauiApp", androidLogLevel);
#endif
}
);
taylor 有一个用于登录 MAUI 的包: https://github.com/roubachof/MetroLog
包括:
您可以在这里找到 Gerald 的教程: https://www.youtube.com/watch?v=WcmnH72kf0
NLog.Targets.MauiLog 支持在各种平台上进行调试日志记录:
我使用了MetroLog.Maui 这是我录制的视频 https://youtu.be/8CPXDHQlBa0
在实用程序类中
private static ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddStreamingFileLogger(options =>
{
options.RetainDays = 2;
options.FolderPath = Path.Combine(FileSystem.CacheDirectory, "InfoBoardLogs");
}));
public static ILogger Logger(string categoryName) {
return loggerFactory.CreateLogger(categoryName);
}
//Usege
using Microsoft.Extensions.Logging;
private readonly ILogger _logger;
_logger = Utilities.Logger(nameof(ImageDisplay));
_logger.LogInformation("\n++++++++++++++ ImageDisplay Constructor");