给微软的日志框架写一个基于委托的日志提供者
動(dòng)手造輪子:給微軟的日志框架寫(xiě)一個(gè)基于委托的日志提供者
Intro
微軟的日志框架現(xiàn)在已經(jīng)比較通用,有時(shí)候我們不想使用外部的日志提供者,但又希望提供一個(gè)比較簡(jiǎn)單的委托就可以實(shí)現(xiàn)日志記錄,于是就有了后面的探索和實(shí)現(xiàn)。
Solution
基于委托的 LoggerProvider 實(shí)現(xiàn)代碼:
自定義 Logger
微軟的日志框架中記錄日志是通過(guò) ILogger 來(lái)做的,擴(kuò)展支持其他日志框架的時(shí)候也需要實(shí)現(xiàn)相應(yīng)的 ILogger 來(lái)適配
ILoggerProvider 會(huì)需要實(shí)現(xiàn)創(chuàng)建 ILogger 的接口 CreateLogger(stringcategoryName) ,所以我們可以先來(lái)實(shí)現(xiàn)對(duì)應(yīng)的 ILogger,實(shí)現(xiàn)如下:
這里的實(shí)現(xiàn)簡(jiǎn)單化處理,默認(rèn)處理所有級(jí)別的日志,如果要設(shè)定日志級(jí)別可以通過(guò)日志的 Fliter 來(lái)做,這里就不做檢查和限制了
private class DelegateLogger : ILogger {private readonly string _categoryName;private readonly Action<string, LogLevel, Exception, string> _logAction;public DelegateLogger(string categoryName, Action<string, LogLevel, Exception, string> logAction){_categoryName = categoryName;_logAction = logAction;}public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter){if (null != _logAction){var msg = formatter(state, exception);_logAction.Invoke(_categoryName, logLevel, exception, msg);}}public bool IsEnabled(LogLevel logLevel){return true;}public IDisposable BeginScope<TState>(TState state){return NullScope.Instance;} }自定義?ILoggerProvider
有了 ILogger 之后接著我們來(lái)定義我們的 ILoggerProvider,這里我們針對(duì) Logger 名稱(chēng)(CategoryName)做了區(qū)分,如果不關(guān)注 Logger 名稱(chēng)的話(huà)也可以使用同一個(gè) Logger 這樣就不需要下面的并發(fā)字典了,只用一個(gè) ILogger 對(duì)象就可以了,可以根據(jù)自己的需要進(jìn)行定制
實(shí)現(xiàn)代碼如下:
[ProviderAlias("Delegate")] public class DelegateLoggerProvider : ILoggerProvider {private readonly Action<string, LogLevel, Exception, string> _logAction;private readonly ConcurrentDictionary<string, DelegateLogger> _loggers = new ConcurrentDictionary<string, DelegateLogger>();public DelegateLoggerProvider(Action<string, LogLevel, Exception, string> logAction){_logAction = logAction;}public void Dispose(){_loggers.Clear();}public ILogger CreateLogger(string categoryName){return _loggers.GetOrAdd(categoryName, category => new DelegateLogger(category, _logAction));} }定義擴(kuò)展方法
為了方便使用,我們需要定義兩個(gè)擴(kuò)展方法來(lái)優(yōu)化我們使用的方式:
定義 ILoggerFactory 的擴(kuò)展方法:
/// <summary> /// AddDelegateLoggerProvider /// </summary> /// <param name="loggerFactory">loggerFactory</param> /// <param name="logAction">logAction</param> /// <returns>loggerFactory</returns> public static ILoggerFactory AddDelegateLogger(this ILoggerFactory loggerFactory, Action<string, LogLevel, Exception, string> logAction) {loggerFactory.AddProvider(new DelegateLoggerProvider(logAction));return loggerFactory; }定義 ILoggingBuilder 的擴(kuò)展方法:
public static ILoggingBuilder AddDelegateLogger(this ILoggingBuilder loggingBuilder,Action<string, LogLevel, Exception, string> logAction) {return loggingBuilder.AddProvider(new DelegateLoggerProvider(logAction)); }使用示例
ILoggerFactory loggerFactory = new LoggerFactory(); //loggerFactory.AddConsole(); loggerFactory.AddDelegateLogger((category, logLevel, exception, msg) =>{Console.WriteLine($"{category}:[{logLevel}] {msg}\n{exception}");} );日志輸出效果如下:
Reference
https://github.com/WeihanLi/WeihanLi.Common/blob/dev/src/WeihanLi.Common/Logging/MicrosoftLoggingLoggerExtensions.cs
https://github.com/WeihanLi/WeihanLi.Common/blob/dev/samples/DotNetCoreSample/Program.cs#L83
總結(jié)
以上是生活随笔為你收集整理的给微软的日志框架写一个基于委托的日志提供者的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: .NET5来了你别慌
- 下一篇: 十问十答 CDDL 许可证