登录 .net MAUI

Sim*_*kas 16 logging log4net nlog maui

.net MAUI 最近在其最新版本之一中删除了日志记录。现在有什么替代方案以及应该如何实施?已经在网上查遍了,但找不到任何实现的日志架构的示例。尝试过log4netNLog但最终没有成功设置其中任何一个。在线有 0 个用于在 MAUI 上设置任何日志记录的示例。

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

现在应该如何在 MAUI 中设置基本日志记录?

PEK*_*PEK 18

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

<ItemGroup Condition="'$(Configuration)' == 'Debug'">
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

然后当应用程序启动时:

var builder = MauiApp.CreateBuilder();
// ...
#if DEBUG
builder.Services.AddLogging(
    configure =>
    {
        configure.AddDebug();
    }
);
#endif
Run Code Online (Sandbox Code Playgroud)

您还可以添加过滤器:

#if DEBUG
builder.Services.AddLogging(
    configure =>
    {
        configure
            .AddDebug()
            .AddFilter("MyCompany.MyApp.Namespace", LogLevel.Trace)
            .AddFilter("Microsoft", LogLevel.Warning);
    }
);
#endif
Run Code Online (Sandbox Code Playgroud)

Android 日志记录(Logcat)

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

builder.Services.AddLogging(
    configure =>
    {
        configure.AddDebug();
        configure.AddConsole();
    }
);
Run Code Online (Sandbox Code Playgroud)

虽然这有效,但日志有点难以阅读。更好的方法是使用包装本机日志记录支持的自定义日志记录提供程序。在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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后你像这样配置它:

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

    }
);
Run Code Online (Sandbox Code Playgroud)