一 简介

无论是微服务还是其他任何分布式系统,都需要一个统一处理日志的系统,这个系统

必须有收集,索引,分析查询的功能。asp

.net core自己的日志是同步方式的,正如文档所言:

所以必须自己提供一个日志提供程序,那正如文档所言,还有什么比kafka更合适的呢。

从kafka往后那就是elasticsearch      kibana,那是自然而然的事情。

二 asp.net core 日志详解

概念

类别:类别是以使用的用途进行分类的,比如var log= LoggerFac.CreateLogger<Startup>();这一句,

以StartUp类的全名作为一个分类,还有一些内置的system,Microsoft,主要是为了更细粒度控制日志。

如果StartUp类出现了问题,打印日志就可以控制在StartUp这一类别下,为这个类别设置debug,

仅仅打印这个类别这个级别的日志信息,使用filter功能可以很容易控制。

日志级别:Trace,debug,info,warning,error等

Trace:因为可以看到组件内部运行状况,而且会有很大安全隐患,所以不建议在生产开启这个功能,

比如mysql lib库的trace可以打印出数据库连接字符串的。

日志提供程序:日志提供程序可以看作日志信息的的io重定向。

控制台:这个不用说了。

调试:就是debug,在 Linux 中,此提供程序将日志写入 /var/log/message。

在windows中就是经典的System.Diagnostics.Debug功能,这是.net提供的调试功能,

非常详细,在开发中非常有用,而且通过配置可以自定义存储,比如一个log文件。

下面是vs中最常见的。

EventSource 提供程序在windows下可用,在linux下没可用但是没有相关事件,所以和没用一样。

Windows EventLog 提供程序和TraceSource 提供程序都是在windows环境下是使用。

三 开发

添加各种事件提供程序:因为是windows下所以EventSource事件是可以用的。

 var host = new WebHostBuilder().ConfigureAppConfiguration((webHostBuild,configBuild) =>
{
var env = webHostBuild.HostingEnvironment; configBuild.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json"
,optional:true,reloadOnChange:true)
.SetBasePath(Directory.GetCurrentDirectory());
}).ConfigureLogging((hostingContext, logging) => {
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug()
.AddEventSourceLogger();
}).UseKestrel()
.UseStartup<Startup>();
host.Start();
Console.ReadKey();
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

日志配置:默认日志debug,system分类info级别,Microsoft分类是info级别。

这个LogLevel下的节点就是日志筛选功能。

{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

windows运行效果:只给加console

testtesttest
dbug: Walt.TestMcroServoces.Webapi.Startup[]
服务配置完成
dbug: Microsoft.AspNetCore.Hosting.Internal.WebHost[]
Hosting starting
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
User profile is available. Using 'C:\Users\Administrator\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
dbug: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[]
Reading data from file 'C:\Users\Administrator\AppData\Local\ASP.NET\DataProtection-Keys\key-7fc5773e-c3fa-4523-b57f-aee522ecc0c2.xml'.
dbug: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[]
Reading data from file 'C:\Users\Administrator\AppData\Local\ASP.NET\DataProtection-Keys\key-b6c77378-6ca9-4d96-a5e2-f5952a8c2a6f.xml'.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
Found key {7fc5773e-c3fa--b57f-aee522ecc0c2}.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
Found key {b6c77378-6ca9-4d96-a5e2-f5952a8c2a6f}.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[]
Considering key {7fc5773e-c3fa--b57f-aee522ecc0c2} with expiration date -- ::30Z as default key.
dbug: Microsoft.AspNetCore.DataProtection.TypeForwardingActivator[]
Forwarded activator type request from Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor, Microsoft.AspNetCore.DataProtection, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 to Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor, Microsoft.AspNetCore.DataProtection, Culture=neutral, PublicKeyToken=adb9793829ddae60
dbug: Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor[]
Decrypting secret element using Windows DPAPI.
dbug: Microsoft.AspNetCore.DataProtection.TypeForwardingActivator[]
Forwarded activator type request from Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=2.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 to Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Culture=neutral, PublicKeyToken=adb9793829ddae60
dbug: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngCbcAuthenticatedEncryptorFactory[]
Opening CNG algorithm 'AES' from provider '(null)' with chaining mode CBC.
dbug: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngCbcAuthenticatedEncryptorFactory[]
Opening CNG algorithm 'SHA256' from provider '(null)' with HMAC.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[]
Using key {7fc5773e-c3fa--b57f-aee522ecc0c2} as the default key.
dbug: Microsoft.AspNetCore.DataProtection.Internal.DataProtectionStartupFilter[]
Key ring with default key {7fc5773e-c3fa--b57f-aee522ecc0c2} was loaded during application startup.
info: Walt.TestMcroServoces.Webapi.Startup[]
infomation
dbug: Microsoft.AspNetCore.Mvc.MvcJsonOptions[]
Compatibility switch AllowInputFormatterExceptionMessages in type MvcJsonOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch AllowCombiningAuthorizeFilters in type MvcOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch AllowBindingHeaderValuesToNonStringModelTypes in type MvcOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch AllowValidatingTopLevelNodes in type MvcOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch InputFormatterExceptionPolicy in type MvcOptions is using compatibility value MalformedInputExceptions for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch SuppressBindingUndefinedValueToEnumType in type MvcOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions[]
Compatibility switch AllowAreas in type RazorPagesOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions[]
Compatibility switch AllowMappingHeadRequestsToGetHandler in type RazorPagesOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcViewOptions[]
Compatibility switch SuppressTempDataAttributePrefix in type MvcViewOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory[]
Registered model binder providers, in the following order: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BinderTypeModelBinderProvider

linux下docker运行效果:debug和 console

testtesttest
dbug: Microsoft.AspNetCore.Hosting.Internal.WebHost[]
Hosting starting
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[]
Repository contains no viable default key. Caller should generate a key with immediate activation.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[]
Policy resolution states that a new key should be added to the key ring.
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
Creating key {0fe5f8aa---83aa-59df0ed4f0c8} with creation date -- ::09Z, activation date -- ::09Z, and expiration date -- ::09Z.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
Descriptor deserializer type for key {0fe5f8aa---83aa-59df0ed4f0c8} is 'Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
No key escrow sink found. Not writing key {0fe5f8aa---83aa-59df0ed4f0c8} to escrow.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
No XML encryptor configured. Key {0fe5f8aa---83aa-59df0ed4f0c8} may be persisted to storage in unencrypted form.
info: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[]
Writing data to file '/root/.aspnet/DataProtection-Keys/key-0fe5f8aa-0875-4612-83aa-59df0ed4f0c8.xml'.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
Key cache expiration token triggered by 'CreateNewKey' operation.
dbug: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[]
Reading data from file '/root/.aspnet/DataProtection-Keys/key-0fe5f8aa-0875-4612-83aa-59df0ed4f0c8.xml'.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
Found key {0fe5f8aa---83aa-59df0ed4f0c8}.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[]
Considering key {0fe5f8aa---83aa-59df0ed4f0c8} with expiration date -- ::09Z as default key.
dbug: Microsoft.AspNetCore.DataProtection.TypeForwardingActivator[]
Forwarded activator type request from Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60 to Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Culture=neutral, PublicKeyToken=adb9793829ddae60
dbug: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory[]
Using managed symmetric algorithm 'System.Security.Cryptography.Aes'.
dbug: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory[]
Using managed keyed hash algorithm 'System.Security.Cryptography.HMACSHA256'.
dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[]
Using key {0fe5f8aa---83aa-59df0ed4f0c8} as the default key.
dbug: Microsoft.AspNetCore.DataProtection.Internal.DataProtectionStartupFilter[]
Key ring with default key {0fe5f8aa---83aa-59df0ed4f0c8} was loaded during application startup.
info: Walt.TestMcroServoces.Webapi.Startup[]
infomation
dbug: Microsoft.AspNetCore.Mvc.MvcJsonOptions[]
Compatibility switch AllowInputFormatterExceptionMessages in type MvcJsonOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch AllowCombiningAuthorizeFilters in type MvcOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch AllowBindingHeaderValuesToNonStringModelTypes in type MvcOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch AllowValidatingTopLevelNodes in type MvcOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch InputFormatterExceptionPolicy in type MvcOptions is using compatibility value MalformedInputExceptions for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
Compatibility switch SuppressBindingUndefinedValueToEnumType in type MvcOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions[]
Compatibility switch AllowAreas in type RazorPagesOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions[]
Compatibility switch AllowMappingHeadRequestsToGetHandler in type RazorPagesOptions is using compatibility value True for version Version_2_1
dbug: Microsoft.AspNetCore.Mvc.MvcViewOptions[]
Compatibility switch SuppressTempDataAttributePref

总结:asp.net core中集成了很多以前.net的日志和调试功能,有点混乱,linux目前能用的提供程序有console和debug,EventSource和事件查看器还有Trace只能在windows专用。上面日志有一些区别,所以加debug提供程序,在相同分类和都具有console提供程序下debug会多出一些信息,所以并不是每个提供程序都共享所有日志信息,根据提供程序不同,会有多余的一些日志信息被加进来。但是如上图所示,并不影响,因为仅仅是一些系统级别的debug提供程序会多出一些信息,mvc模块给出的信息完全一致。

asp.net core mcroservices 架构之 分布式日志(一)的更多相关文章

  1. asp.net core mcroservices 架构之 分布式日志(三):集成kafka

    一 kafka介绍 kafka是基于zookeeper的一个分布式流平台,既然是流,那么大家都能猜到它的存储结构基本上就是线性的了.硬盘大家都知道读写非常的慢,那是因为在随机情况下,线性下,硬盘的读写 ...

  2. asp.net core mcroservices 架构之 分布式日志(二)之自定义日志开发

    netcore日志原理 netcore的日志是作为一个扩展库存在的,每个组件都有它的入口,那么作为研究这个组件的入口是最好的,首先看两种方式: 这个是源码例子提供的. var loggingConfi ...

  3. asp.net core microservices 架构之分布式自动计算(三)-kafka日志同步至elasticsearch和kibana展示

    一 kafka consumer准备 前面的章节进行了分布式job的自动计算的概念讲解以及实践.上次分布式日志说过日志写进kafka,是需要进行处理,以便合理的进行展示,分布式日志的量和我们对日志的重 ...

  4. asp.net core microservices 架构之 分布式自动计算(二)

    一  简介                   上一篇介绍了zookeeper如何进行分布式协调,这次主要讲解quartz使用zookeeper进行分布式计算,因为上一篇只是讲解原理,而这次实际使用, ...

  5. asp.net core microservices 架构之 分布式自动计算(一)

       一:简介   自动计算都是常驻内存的,没有人机交互.我们经常用到的就是console job和sql job了.sqljob有自己的宿主,与数据库产品有很关联,暂时不提.console job使 ...

  6. .net core microservices 架构之 分布式

    .net core microservices 架构之 分布式  一:简介   自动计算都是常驻内存的,没有人机交互.我们经常用到的就是console job和sql job了.sqljob有自己的宿 ...

  7. ASP.NET Core使用Elasticsearch记录NLog日志

    ASP.NET Core使用Elasticsearch记录NLog日志 1.新建一个 ASP.NET Core项目 2.安装Nuge包 运行:Install-Package NLog.Web.AspN ...

  8. ASP.NET Core 使用 Redis 实现分布式缓存:Docker、IDistributedCache、StackExchangeRedis

    ASP.NET Core 使用 Redis 实现分布式缓存:Docker.IDistributedCache.StackExchangeRedis 前提:一台 Linux 服务器.已安装 Docker ...

  9. asp.net core microservices 架构之eureka服务发现

    一 简介 微服务将需多的功能拆分为许多的轻量级的子应用,这些子应用相互调度.好处就是轻量级,完全符合了敏捷开发的精神.我们知道ut(单元测试),不仅仅提高我们的程序的健壮性,而且可以强制将类和方法的设 ...

随机推荐

  1. DOM加载过程

    静态的dom   动态的dom             http://blog.csdn.net/cxiaokai/article/details/7552653     一:预编译   解释 js加 ...

  2. Python异步非阻塞IO多路复用Select/Poll/Epoll使用,线程,进程,协程

    1.使用select模拟socketserver伪并发处理客户端请求,代码如下: import socket import select sk = socket.socket() sk.bind((' ...

  3. Redis的分布式配置

    Redis存在三种级别的分布式部署:主从复制.主从切换.集群配置,推荐使用主从切换模式. 主从复制 启动主服务:端口6379 启动从服务:端口6380, 配置文件中加上 slaveof 127.0.0 ...

  4. ETL应用:使用Pro*C写入文件信息入库的方法

    ETL处理过程中,经常需要进行文件校验,如文件级校验.记录级校验,需要保存文件的基本信息,文件名.文件大小.数据日期等,使用Pro*C的一种方法如下: #include <stdio.h> ...

  5. Django框架之cookie和session及开发登录功能

    1.cookie是什么? Web应用程序是使用HTTP协议传输数据的.HTTP协议是无状态的协议.一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的连接.这就意味着服务器无法从 ...

  6. String和StringBuilder、StringBuffer

    Java平台提供了两种类型的字符串:String和StringBuffer/StringBuilder String 只读字符串,这里的只读并不是指String类型变量无法被修改,而是指String类 ...

  7. linux 安装mysql服务

    1.检查是否已安装,grep的-i选项表示匹配时忽略大小写 rpm -qa|grep -i mysql *可见已经安装了库文件,应该先卸载,不然会出现覆盖错误.注意卸:载时使用了--nodeps选项, ...

  8. POI实现数据的导入

    1.POI技术的概述? POI技术:apache POI是可以对微软office文档进行读和写的工具. l HSSF:操作97格式的excel,扩展名:.xls 纯二进制,最大行数65535. l X ...

  9. JSP的动态Include的静态Include

    1. 静态导入示例 先总结: 1:静态include是把被引入的文件拼接到本页面中,再做为一个整体来编译,返回结果给客户端. 动态include是分别编译本页面和被引入的页面,再把结果合成一个html ...

  10. thinkphp URL 模式

    兼容ThinkPHP三种url模式的nginx rewrite location / { root /var/www; index index.html index.htm index.php; if ...