• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

.net core日志系统相关总结

asp 搞代码 4年前 (2022-01-03) 59次浏览 已收录 0个评论

多年的经验,日志记录是软件开发的重要组成部分。没有日志记录机制的系统不是完善的系统。在开发阶段可以通过debug附件进程进行交互调试,可以检测到一些问题,但是在上线之后,日志的记录起到至关重要的作用。本文讲解下日志系统的相关使用

前言

本节开始整理日志相关的东西。先整理一下日志的基本原理。

正文

首先介绍一下包:

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<mark style="color:transparent">来源gaodaimacom搞#^代%!码网</mark> ServiceCollection(); serviceCollection.AddSingleton(p=>config); serviceCollection.AddLogging(builder => { builder.AddConfiguration(config.GetSection("Logging")); builder.AddConsole(); }); IServiceProvider service = serviceCollection.BuildServiceProvider(); ILoggerFactory loggerFactory = service.GetService(); 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 configure) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); services.TryAdd(ServiceDescriptor.Singleton()); services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger), typeof(Logger))); services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions>( 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>(new LoggerFilterConfig

以上就是.net core日志系统相关总结的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:.net core日志系统相关总结

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址