VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • 动手造轮子:写一个日志框架(2)

(ILogHelperLoggingEnricher enricher); /// <summary> /// Add logs filter /// </summary> /// <param name="filterFunc">filterFunc, logProviderType/categoryName/Exception, whether to write log</param> bool AddFilter(Func<Type, string, LogHelperLogLevel, Exception, bool> filterFunc); ///// <summary> ///// config period batching ///// </summary> ///// <param name="period">period</param> ///// <param name="batchSize">batchSize</param> //void PeriodBatchingConfig(TimeSpan period, int batchSize); /// <summary> /// Build for LogFactory /// </summary> /// <returns></returns> ILogHelperFactory Build(); }

增加 logging 的配置:


	
Copy
public static class LogHelper { private static ILogHelperFactory LogFactory { get; private set; } = NullLogHelperFactory.Instance; public static void ConfigureLogging(Action<ILogHelperLoggingBuilder> configureAction) { var loggingBuilder = new LogHelperLoggingBuilder(); configureAction?.Invoke(loggingBuilder); LogFactory = loggingBuilder.Build(); } public static ILogHelperLogger GetLogger<T>() => LogFactory.GetLogger(typeof(T)); public static ILogHelperLogger GetLogger(Type type) => LogFactory.GetLogger(type); public static ILogHelperLogger GetLogger(string categoryName) { return LogFactory.CreateLogger(categoryName); } }

最后的使用方式:


	
Copy
internal class LoggingTest { private static readonly ILogHelperLogger Logger = LogHelper.GetLogger<LoggingTest>(); public static void MainTest() { var abc = "1233"; LogHelper.ConfigureLogging(builder => { builder .AddLog4Net() //.AddSerilog(loggerConfig => loggerConfig.WriteTo.Console()) .WithMinimumLevel(LogHelperLogLevel.Info) .WithFilter((category, level) => level > LogHelperLogLevel.Error && category.StartsWith("System")) .EnrichWithProperty("Entry0", ApplicationHelper.ApplicationName) .EnrichWithProperty("Entry1", ApplicationHelper.ApplicationName, e => e.LogLevel >= LogHelperLogLevel.Error)// 当 LogLevel 是 Error 及以上级别时才增加 Property ; }); Logger.Debug("12333 {abc}", abc); Logger.Trace("122334334"); Logger.Info($"122334334 {abc}"); Logger.Warn("12333, err:{err}", "hahaha"); Logger.Error("122334334"); Logger.Fatal("12333"); } }

More#

增加 LoggingEvent 还想做一个批量提交日志,如上面定义的 PeriodBatchingConfig 一样,批量同步到 Provider 但是实际使用下来,有些 provider 不支持设置日志的时间,时间是内部记录的,这样一来日志记录的时间就不准了,而且大多都不支持批量写日志,所以后面放弃了,但是如果只是用自己的扩展,不用 log4net 之类的外部的日志框架的话,我觉得还是可以做的,可以提高效率,目前主要用 Serilog 和 log4net,暂时不更新了,就先这样吧

下一版本要解决的事情

  • ILogProvider 记录日志返回一个 Task 感觉有些鸡肋,没太大意义,后面再改一下吧
  • Serilog 的 Filter 是基于 LogEvent 的,后面看是否需要改一下,基于 LogEvent 的话更简洁,而且可以根据 LogEvent 内的 Properties 做过滤,所以 AddFilter 的API 可以更新一下 AddFilter(Func<LogHelperLoggingEvent, bool> filter)

Reference#

  • https://github.com/serilog/serilog
  • https://github.com/serilog/serilog-sinks-periodicbatching
  • https://github.com/aspnet/Logging
  • https://github.com/aspnet/Extensions/tree/master/src/Logging
  • https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common/Logging

作者: WeihanLi



相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号