基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 574a6a769115034f37bdecf3284545d74843816a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System.IO;
using System.Text;
using Microsoft.Extensions.Logging;
 
namespace Moli.Desktop.Diagnostics;
 
public sealed class DailyFileLoggerProvider : ILoggerProvider
{
    private readonly string _logDirectory;
    private readonly object _gate = new();
    private bool _disposed;
 
    public DailyFileLoggerProvider(string logDirectory)
    {
        _logDirectory = logDirectory;
        Directory.CreateDirectory(_logDirectory);
    }
 
    public ILogger CreateLogger(string categoryName)
        => new DailyFileLogger(categoryName, _logDirectory, _gate, () => _disposed);
 
    public void Dispose()
    {
        _disposed = true;
    }
 
    private sealed class DailyFileLogger(
        string categoryName,
        string logDirectory,
        object gate,
        Func<bool> isDisposed) : ILogger
    {
        public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
 
        public bool IsEnabled(LogLevel logLevel)
            => logLevel != LogLevel.None && !isDisposed();
 
        public void Log<TState>(
            LogLevel logLevel,
            EventId eventId,
            TState state,
            Exception? exception,
            Func<TState, Exception?, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }
 
            var message = formatter(state, exception);
            if (string.IsNullOrWhiteSpace(message) && exception is null)
            {
                return;
            }
 
            var now = DateTimeOffset.Now;
            var line = $"{now:O} [{logLevel}] {categoryName} {message}";
            if (exception is not null)
            {
                line += $" {exception.GetType().Name}: {exception.Message}";
            }
 
            var path = Path.Combine(logDirectory, $"moli-desktop-{now:yyyyMMdd}.log");
            try
            {
                lock (gate)
                {
                    File.AppendAllText(path, line + Environment.NewLine, Encoding.UTF8);
                }
            }
            catch (IOException)
            {
            }
            catch (UnauthorizedAccessException)
            {
            }
        }
    }
}