.net MAUI最近删除了它的最新版本之一的日志记录。现在有什么可供选择的,应该如何实施呢?一直在网上运行,但是却找不到任何实现的日志记录架构的一个例子。尝试了log4net,NLog,但最终没有成功地设置任何一个。有0例在线设置它在毛伊岛的任何日志记录。
此外,还看到了builder.Services.AddLogging()和builder.Logging.Services在MauiProgram中,它们应该与依赖注入一起工作,但也找不到任何用于该实现的Maui示例。
现在应该如何在毛伊岛建立一个基本的日志记录呢?
发布于 2022-04-02 07:11:17
首先添加对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);
});
#endifAndroid日志记录(Logcat)
如果您想在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
});发布于 2022-09-18 09:30:18
NLog.Targets.MauiLog在各种平台上启用调试日志记录:
https://stackoverflow.com/questions/71599950
复制相似问题