- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章.net core日志系统相关总结由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本节开始整理日志相关的东西。先整理一下日志的基本原理.
。
首先介绍一下包
1.Microsoft.Extengsion.Logging.Abstrations 。
这个是接口包.
2.Microsoft.Extengsion.Logging 。
这个是实现包 。
3.Microsoft.Extengsion.Logging.Console 。
这个是扩展包 。
代码如下
static void Main(string[] args){ IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.AddJsonFile("appsettings.json",optional:false,reloadOnChange:true); var config = configurationBuilder.Build(); IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton<IConfiguration>(p=>config); serviceCollection.AddLogging(builder => { builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole(); }); IServiceProvider service = serviceCollection.BuildServiceProvider(); ILoggerFactory loggerFactory = service.GetService<ILoggerFactory>(); var loggerObj = loggerFactory.CreateLogger("Default"); loggerObj.LogInformation(2021, "Default,now that is 2021"); var loggerObj2 = loggerFactory.CreateLogger("loggerObj"); loggerObj2.LogDebug(2021, "loggerObj,now that is 2021"); Console.ReadKey();}
配置文件
{"Logging": { "LogLevel": { "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }, "Console": { "LogLevel": { "Default": "Information", "Program": "Trace", "loggerObj": "Debug" } }}}
结果
首先是配置级别的问题,查看loglevel 文件
public enum LogLevel{/// <summary>Logs that contain the most detailed messages. These messages may contain sensitive application data./// These messages are disabled by default and should never be enabled in a production environment.</summary>Trace,/// <summary>Logs that are used for interactive investigation during development. These logs should primarily contain/// information useful for debugging and have no long-term value.</summary>Debug,/// <summary>Logs that track the general flow of the application. These logs should have long-term value.</summary>Information,/// <summary>Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the/// application execution to stop.</summary>Warning,/// <summary>Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a/// failure in the current activity, not an application-wide failure.</summary>Error,/// <summary>Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires/// immediate attention.</summary>Critical,/// <summary>Not used for writing log messages. Specifies that a logging category should not write any messages.</summary>None,}
从上之下,依次提高log级别.
比如说设置了log 级别是Error,那么Debug、Information、Warning 都不会被答应出来.
那么就来分析一下代码吧.
AddLogging
public static IServiceCollection AddLogging(this IServiceCollection services, Action<ILoggingBuilder> configure){ if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>()); services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>))); services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<LoggerFilterOptions>>( new DefaultLoggerLevelConfigureOptions(LogLevel.Information))); configure(new LoggingBuilder(services)); return services;}
这里面给注册了ILoggerFactory和ILogger。然后设置了一个打印log的级别配置,LogLevel.Information,这个就是如果我们没有配置文件默认就是Information这种级别了.
configure(new LoggingBuilder(services)) 给我们的委托提供了一个LoggingBuilder的实例化对象。这个对象就是用来专门做扩展的,是解耦的一种方式.
internal class LoggingBuilder : ILoggingBuilder{ public LoggingBuilder(IServiceCollection services) { Services = services; } public IServiceCollection Services { get; }}
这个LoggingBuilder 类基本什么功能都没有,但是因为有了这样一个类,就可以作为扩展的标志了.
比如说上文的
builder.AddConfiguration(config.GetSection("Logging"));builder.AddConsole();
看下AddConfiguration
public static ILoggingBuilder AddConfiguration(this ILoggingBuilder builder, IConfiguration configuration){ builder.AddConfiguration(); builder.Services.AddSingleton<IConfigureOptions<LoggerFilterOptions>>(new LoggerFilterConfigureOptions(configuration)); builder.Services.AddSingleton<IOptionsChangeTokenSource<LoggerFilterOptions>>(new ConfigurationChangeTokenSource<LoggerFilterOptions>(configuration)); builder.Services.AddSingleton(new LoggingConfiguration(configuration)); return builder;}
这里面给我们注入了配置文件的配置:builder.Services.AddSingleton<IConfigureOptions>(new LoggerFilterConfigureOptions(configuration)) 。
同时给我们注册监听令牌:builder.Services.AddSingleton<IOptionsChangeTokenSource>(new ConfigurationChangeTokenSource(configuration)),
这里给我们注册配置保存在LoggingConfiguration中:builder.Services.AddSingleton(new LoggingConfiguration(configuration)),
因为LoggingConfiguration 保存了,故而我们随时可以获取到LoggingConfiguration 的配置.
看下AddConsole
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>public static ILoggingBuilder AddConsole(this ILoggingBuilder builder){ builder.AddConfiguration(); builder.AddConsoleFormatter<JsonConsoleFormatter, JsonConsoleFormatterOptions>(); builder.AddConsoleFormatter<SystemdConsoleFormatter, ConsoleFormatterOptions>(); builder.AddConsoleFormatter<SimpleConsoleFormatter, SimpleConsoleFormatterOptions>(); builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, ConsoleLoggerProvider>()); LoggerProviderOptions.RegisterProviderOptions<ConsoleLoggerOptions, ConsoleLoggerProvider>(builder.Services); return builder;}
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, ConsoleLoggerProvider>()) 里面给我们ILoggerProvider 增加了一个ConsoleLoggerProvider,故而我们多了一个打印的功能.
LoggerProviderOptions.RegisterProviderOptions<ConsoleLoggerOptions, ConsoleLoggerProvider>(builder.Services) 给我们加上了ConsoleLoggerOptions 绑定为ConsoleLoggerProvider的配置.
RegisterProviderOptions 如下
public static void RegisterProviderOptions<TOptions, TProvider>(IServiceCollection services) where TOptions : class{ services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<TOptions>, LoggerProviderConfigureOptions<TOptions, TProvider>>()); services.TryAddEnumerable(ServiceDescriptor.Singleton<IOptionsChangeTokenSource<TOptions>, LoggerProviderOptionsChangeTokenSource<TOptions, TProvider>>());}
接下来就是调用服务
var loggerObj = loggerFactory.CreateLogger("Default");loggerObj.LogInformation(2021, "Default,now that is 2021");
看下LoggerFactory的CreateLogger:
public ILogger CreateLogger(string categoryName){ if (CheckDisposed()) { throw new ObjectDisposedException(nameof(LoggerFactory)); } lock (_sync) { if (!_loggers.TryGetValue(categoryName, out Logger logger)) { logger = new Logger { Loggers = CreateLoggers(categoryName), }; (logger.MessageLoggers, logger.ScopeLoggers) = ApplyFilters(logger.Loggers); _loggers[categoryName] = logger; } return logger; }}
里面做了缓存,如果categoryName有缓存的话直接使用缓存,如果没有那么调用CreateLoggers创建.
查看CreateLoggers
private LoggerInformation[] CreateLoggers(string categoryName){ var loggers = new LoggerInformation[_providerRegistrations.Count]; for (int i = 0; i < _providerRegistrations.Count; i++) { loggers[i] = new LoggerInformation(_providerRegistrations[i].Provider, categoryName); } return loggers;}
这里面就用我们前面注册过的全部logger的provider,封装进LoggerInformation.
查看LoggerInformation
internal readonly struct LoggerInformation{ public LoggerInformation(ILoggerProvider provider, string category) : this() { ProviderType = provider.GetType(); Logger = provider.CreateLogger(category); Category = category; ExternalScope = provider is ISupportExternalScope; } public ILogger Logger { get; } public string Category { get; } public Type ProviderType { get; } public bool ExternalScope { get; }}
里面调用了我们,每个provider的CreateLogger.
那么这个时候我们就找一个provider 看下CreateLogger到底做了什么,这里就找一下ConsoleLoggerProvider,因为我们添加了这个.
[ProviderAlias("Console")]public class ConsoleLoggerProvider : ILoggerProvider, ISupportExternalScope{ private readonly IOptionsMonitor<ConsoleLoggerOptions> _options; public ILogger CreateLogger(string name) { if (_options.CurrentValue.FormatterName == null || !_formatters.TryGetValue(_options.CurrentValue.FormatterName, out ConsoleFormatter logFormatter)) {#pragma warning disable CS0618 logFormatter = _options.CurrentValue.Format switch { ConsoleLoggerFormat.Systemd => _formatters[ConsoleFormatterNames.Systemd], _ => _formatters[ConsoleFormatterNames.Simple], }; if (_options.CurrentValue.FormatterName == null) { UpdateFormatterOptions(logFormatter, _options.CurrentValue); }#pragma warning disable CS0618 } return _loggers.GetOrAdd(name, loggerName => new ConsoleLogger(name, _messageQueue) { Options = _options.CurrentValue, ScopeProvider = _scopeProvider, Formatter = logFormatter, }); }}
看到这个IOptionsMonitor,就知道console 配置是支持热更新的,里面创建了ConsoleLogger,这个ConsoleLogger就是用来打log正在的调用类.
值得注意的是_messageQueue这个,看了打印log还是有一个队列的,按照先进先出原则.
那么最后来看一下loggerObj.LogInformation(2021, "Default,now that is 2021");
第一层public static void LogInformation(this ILogger logger, EventId eventId, string message, params object[] args){ logger.Log(LogLevel.Information, eventId, message, args);}第二层public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, string message, params object[] args){ logger.Log(logLevel, eventId, null, message, args);}第三层public static void Log(this ILogger logger, LogLevel logLevel, EventId eventId, Exception exception, string message, params object[] args){ if (logger == null) { throw new ArgumentNullException(nameof(logger)); } logger.Log(logLevel, eventId, new FormattedLogValues(message, args), exception, _messageFormatter);}
那么这个logger.Log 是调用具体某个logger,像consoleLogger 吗? 不是,我们看LoggerFactory的CreateLogger时候封装了:
logger = new Logger{ Loggers = CreateLoggers(categoryName),};
那么看下Logger的Log到底干了什么.
internal class Logger : ILogger{ public LoggerInformation[] Loggers { get; set; } public MessageLogger[] MessageLoggers { get; set; } public ScopeLogger[] ScopeLoggers { get; set; } public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) { MessageLogger[] loggers = MessageLoggers; if (loggers == null) { return; } List<Exception> exceptions = null; for (int i = 0; i < loggers.Length; i++) { ref readonly MessageLogger loggerInfo = ref loggers[i]; if (!loggerInfo.IsEnabled(logLevel)) { continue; } LoggerLog(logLevel, eventId, loggerInfo.Logger, exception, formatter, ref exceptions, state); } if (exceptions != null && exceptions.Count > 0) { ThrowLoggingError(exceptions); } static void LoggerLog(LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func<TState, Exception, string> formatter, ref List<Exception> exceptions, in TState state) { try { logger.Log(logLevel, eventId, state, exception, formatter); } catch (Exception ex) { if (exceptions == null) { exceptions = new List<Exception>(); } exceptions.Add(ex); } } }}
里面循环判断是否当前级别能够输出:!loggerInfo.IsEnabled(logLevel) 。
然后调用对应的具体ILog实现的Log,这里贴一下ConsoleLogger 的实现
[ThreadStatic]private static StringWriter t_stringWriter;public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter){ if (!IsEnabled(logLevel)) { return; } if (formatter == null) { throw new ArgumentNullException(nameof(formatter)); } t_stringWriter ??= new StringWriter(); LogEntry<TState> logEntry = new LogEntry<TState>(logLevel, _name, eventId, state, exception, formatter); Formatter.Write(in logEntry, ScopeProvider, t_stringWriter); var sb = t_stringWriter.GetStringBuilder(); if (sb.Length == 0) { return; } string computedAnsiString = sb.ToString(); sb.Clear(); if (sb.Capacity > 1024) { sb.Capacity = 1024; } _queueProcessor.EnqueueMessage(new LogMessageEntry(computedAnsiString, logAsError: logLevel >= Options.LogToStandardErrorThreshold));}
把这个队列的也贴一下,比较经典吧.
internal class ConsoleLoggerProcessor : IDisposable{ private const int _maxQueuedMessages = 1024; private readonly BlockingCollection<LogMessageEntry> _messageQueue = new BlockingCollection<LogMessageEntry>(_maxQueuedMessages); private readonly Thread _outputThread; public IConsole Console; public IConsole ErrorConsole; public ConsoleLoggerProcessor() { // Start Console message queue processor _outputThread = new Thread(ProcessLogQueue) { IsBackground = true, Name = "Console logger queue processing thread" }; _outputThread.Start(); } public virtual void EnqueueMessage(LogMessageEntry message) { if (!_messageQueue.IsAddingCompleted) { try { _messageQueue.Add(message); return; } catch (InvalidOperationException) { } } // Adding is completed so just log the message try { WriteMessage(message); } catch (Exception) { } } // for testing internal virtual void WriteMessage(LogMessageEntry entry) { IConsole console = entry.LogAsError ? ErrorConsole : Console; console.Write(entry.Message); } private void ProcessLogQueue() { try { foreach (LogMessageEntry message in _messageQueue.GetConsumingEnumerable()) { WriteMessage(message); } } catch { try { _messageQueue.CompleteAdding(); } catch { } } } public void Dispose() { _messageQueue.CompleteAdding(); try { _outputThread.Join(1500); // with timeout in-case Console is locked by user input } catch (ThreadStateException) { } }}
以上就是.net core日志系统相关总结的详细内容,更多关于.net core日志的资料请关注我其它相关文章! 。
原文链接:https://www.cnblogs.com/aoximin/p/14854519.html 。
最后此篇关于.net core日志系统相关总结的文章就讲到这里了,如果你想了解更多关于.net core日志系统相关总结的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
第一段代码工作正常,并给出了我需要的结果。我现在想做的是让它在 'as num' 上返回 3 个数字值对于“as num”上的 3 个不同值,对于同一列上的 3 个不同位置 SELEC
我想分析一些数据以编写定价算法。以下日期可用: 我需要三个变量/维度的函数/相关因子,它显示三个维度(pers_capacity、卧室数量、浴室数量)增长时中位数(价格)的变化。例如Y(#pers_c
正如标题所说 - 我的 Sprite Kit 游戏时不时地在后台崩溃,总是出现此错误 - Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Sub
假设我尝试保存以下数据,并且Songs模型的name属性上设置了Phalcon \ Mvc \ Model \ Validator \ PresenceOf验证器 // Get an existing
我有一个 if 控件,如下所示; if (Directory.Exists(System.IO.Path.Combine(systemPath, "Reports", companyName))
有人可以告诉我我们使用 ReadLine() 从文件 (.txt) 中读取特定行吗?现在我想读取文件的全部内容(不仅仅是第一行)。为此我需要使用什么方法。我用谷歌搜索了很多,但找不到解决方案。 我的代
我相信在大学时我用从 C 派生的语言为 FPGA 编写了一个程序。我了解 VHDL 和 verilog 等语言。但是,我不明白的是程序员在使用哪个方面有多少选择?它依赖于FPGA吗?我将使用 Xili
我有一个 if 控件,如下所示; if (Directory.Exists(System.IO.Path.Combine(systemPath, "Reports", companyName))
如何在运行时更改 Dashcode (Javascript) 中图像对象的源? 我试过: var image = document.getElementById("image").object;ima
我有几个相互关联的类,它们将被多种不同的算法使用 例子: struct B; struct A { B* parent; }; struct B { std::vector child
我正在开发一个网站,用户在客户收到的表中输入金额,如果任何客户没有提供分期付款(金额),则用户不会在表中输入任何金额,并且用户希望获取违约者的信息客户以10天为基础。所以我的问题是应该定义什么表和属性
我试图从上一个条目中选择一个值,并每次将该数字加一。我让它工作到选择当前条目值(默认 1000)并递增 1 并重新插入该值(因此每次最终都是 1001)。我需要它来选择该字段的最后一个条目,这样它将变
我不擅长“制作”查询。假设这是我的数据库: artist pics ------------------- -
最近,我要为我的网站做一个即时通知系统。我听说 COMET 在这种情况下必不可少。 我已经搜索 PHP 和 Comet 一段时间了,但是,我发现的指南和文章似乎只是循环中的 ajax 请求。例如,有一
我正在开发一款 iOS 游戏,我希望 clown 在场景外生成,然后向下移动。我的想法是全部创建它们,并将它们以 360 像素的距离放置在不可见的场景中。 像这样: SKSpriteNode *clo
我有以下子订单表。 mysql> select * from suborder; +-------------+------------------+ | order_state | bookin
这可能是一个有点初学者的问题,但考虑到在 Java 中调试编码是相当相关的:什么时候编码与 String 对象相关? 假设我有一个要保存到文件中的字符串对象。 String 对象本身是否使用某种我应该
首先我想说我是 CPP 的新手(我从 cpp11 开始):)考虑以下实体:学生(名字+姓氏)和组(描述+更多学生)。我在 C++ 中创建了以下 2 个类: class Student { privat
我正在尝试在单击该复选框时同步更新我的数据库。我决定使用 aJax,但它似乎无法识别 ajax。 代码:将成为 Switch_Active(this.id) 函数的元素 ... Deactivat
我正在创建一个菜单。菜单如下。 $('.category').mouseover(function() { $(this).removeClass('category').addClass('cate
我是一名优秀的程序员,十分优秀!