首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在.net MAUI上登录

在.net MAUI上登录
EN

Stack Overflow用户
提问于 2022-03-24 09:31:28
回答 2查看 3.2K关注 0票数 8

.net MAUI最近删除了它的最新版本之一的日志记录。现在有什么可供选择的,应该如何实施呢?一直在网上运行,但是却找不到任何实现的日志记录架构的一个例子。尝试了log4netNLog,但最终没有成功地设置任何一个。有0例在线设置它在毛伊岛的任何日志记录。

此外,还看到了builder.Services.AddLogging()builder.Logging.Services在MauiProgram中,它们应该与依赖注入一起工作,但也找不到任何用于该实现的Maui示例。

现在应该如何在毛伊岛建立一个基本的日志记录呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-02 07:11:17

首先添加对Microsoft.Extensions.Logging.Debug的引用。如果只希望在调试模式下这样做,您可以这样做:

代码语言:javascript
复制
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
  <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
</ItemGroup>

然后,当应用程序启动时:

代码语言:javascript
复制
var builder = MauiApp.CreateBuilder();
// ...
#if DEBUG
    builder.Services.AddLogging(configure =>
    {
         configure.AddDebug();
    });
#endif

还可以添加筛选器:

代码语言:javascript
复制
#if DEBUG
    builder.Services.AddLogging(configure =>
    {
        configure.AddDebug()
            .AddFilter("MyCompany.MyApp.Namespace", LogLevel.Trace)
            .AddFilter("Microsoft", LogLevel.Warning);
    });
#endif

Android日志记录(Logcat)

如果您想在Android上使用本机日志记录支持,那么一个简单的解决方法是添加对Microsoft.Extensions.Logging.Console的引用,然后配置它:

代码语言:javascript
复制
builder.Services.AddLogging(configure =>
{
    configure.AddDebug();
    configure.AddConsole();
});

虽然这样做有效,但日志还是有点难读。更好的方法是使用包装本机日志记录支持的自定义日志记录提供程序。在Android文件夹中,添加以下代码:

代码语言:javascript
复制
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;
        }
    }
}

然后像这样配置它:

代码语言:javascript
复制
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

});
票数 10
EN

Stack Overflow用户

发布于 2022-09-18 09:30:18

NLog.Targets.MauiLog在各种平台上启用调试日志记录:

  • Apple iOS / MacOS -统一日志记录
  • 安卓- Android.Util.Log / LogCat
  • NetStandard - System.Diagnostics.Debugger.Log
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71599950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档