一 简介

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

必须有收集,索引,分析查询的功能。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事件是可以用的。

  1. var host = new WebHostBuilder().ConfigureAppConfiguration((webHostBuild,configBuild) =>
  2. {
  3. var env = webHostBuild.HostingEnvironment;
  4.  
  5. configBuild.AddJsonFile("appsettings.json")
  6. .AddJsonFile($"appsettings.{env.EnvironmentName}.json"
  7. ,optional:true,reloadOnChange:true)
  8. .SetBasePath(Directory.GetCurrentDirectory());
  9. }).ConfigureLogging((hostingContext, logging) => {
  10. logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"))
  11. .AddConsole()
  12. .AddDebug()
  13. .AddEventSourceLogger();
  14. }).UseKestrel()
  15. .UseStartup<Startup>();
  16. host.Start();
  17. Console.ReadKey();
  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Debug",
  5. "System": "Information",
  6. "Microsoft": "Information"
  7. }
  8. }
  9. }

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

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

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Debug",
  5. "System": "Information",
  6. "Microsoft": "Information"
  7. }
  8. }
  9. }

windows运行效果:只给加console

  1. testtesttest
  2. dbug: Walt.TestMcroServoces.Webapi.Startup[]
  3. 服务配置完成
  4. dbug: Microsoft.AspNetCore.Hosting.Internal.WebHost[]
  5. Hosting starting
  6. info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  7. 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.
  8. dbug: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[]
  9. Reading data from file 'C:\Users\Administrator\AppData\Local\ASP.NET\DataProtection-Keys\key-7fc5773e-c3fa-4523-b57f-aee522ecc0c2.xml'.
  10. dbug: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[]
  11. Reading data from file 'C:\Users\Administrator\AppData\Local\ASP.NET\DataProtection-Keys\key-b6c77378-6ca9-4d96-a5e2-f5952a8c2a6f.xml'.
  12. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  13. Found key {7fc5773e-c3fa--b57f-aee522ecc0c2}.
  14. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  15. Found key {b6c77378-6ca9-4d96-a5e2-f5952a8c2a6f}.
  16. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[]
  17. Considering key {7fc5773e-c3fa--b57f-aee522ecc0c2} with expiration date -- ::30Z as default key.
  18. dbug: Microsoft.AspNetCore.DataProtection.TypeForwardingActivator[]
  19. 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
  20. dbug: Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor[]
  21. Decrypting secret element using Windows DPAPI.
  22. dbug: Microsoft.AspNetCore.DataProtection.TypeForwardingActivator[]
  23. 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
  24. dbug: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngCbcAuthenticatedEncryptorFactory[]
  25. Opening CNG algorithm 'AES' from provider '(null)' with chaining mode CBC.
  26. dbug: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngCbcAuthenticatedEncryptorFactory[]
  27. Opening CNG algorithm 'SHA256' from provider '(null)' with HMAC.
  28. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[]
  29. Using key {7fc5773e-c3fa--b57f-aee522ecc0c2} as the default key.
  30. dbug: Microsoft.AspNetCore.DataProtection.Internal.DataProtectionStartupFilter[]
  31. Key ring with default key {7fc5773e-c3fa--b57f-aee522ecc0c2} was loaded during application startup.
  32. info: Walt.TestMcroServoces.Webapi.Startup[]
  33. infomation
  34. dbug: Microsoft.AspNetCore.Mvc.MvcJsonOptions[]
  35. Compatibility switch AllowInputFormatterExceptionMessages in type MvcJsonOptions is using compatibility value True for version Version_2_1
  36. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  37. Compatibility switch AllowCombiningAuthorizeFilters in type MvcOptions is using compatibility value True for version Version_2_1
  38. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  39. Compatibility switch AllowBindingHeaderValuesToNonStringModelTypes in type MvcOptions is using compatibility value True for version Version_2_1
  40. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  41. Compatibility switch AllowValidatingTopLevelNodes in type MvcOptions is using compatibility value True for version Version_2_1
  42. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  43. Compatibility switch InputFormatterExceptionPolicy in type MvcOptions is using compatibility value MalformedInputExceptions for version Version_2_1
  44. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  45. Compatibility switch SuppressBindingUndefinedValueToEnumType in type MvcOptions is using compatibility value True for version Version_2_1
  46. dbug: Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions[]
  47. Compatibility switch AllowAreas in type RazorPagesOptions is using compatibility value True for version Version_2_1
  48. dbug: Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions[]
  49. Compatibility switch AllowMappingHeadRequestsToGetHandler in type RazorPagesOptions is using compatibility value True for version Version_2_1
  50. dbug: Microsoft.AspNetCore.Mvc.MvcViewOptions[]
  51. Compatibility switch SuppressTempDataAttributePrefix in type MvcViewOptions is using compatibility value True for version Version_2_1
  52. dbug: Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderFactory[]
  53. Registered model binder providers, in the following order: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BinderTypeModelBinderProvider

linux下docker运行效果:debug和 console

  1. testtesttest
  2. dbug: Microsoft.AspNetCore.Hosting.Internal.WebHost[]
  3. Hosting starting
  4. info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  5. User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
  6. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[]
  7. Repository contains no viable default key. Caller should generate a key with immediate activation.
  8. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[]
  9. Policy resolution states that a new key should be added to the key ring.
  10. info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  11. Creating key {0fe5f8aa---83aa-59df0ed4f0c8} with creation date -- ::09Z, activation date -- ::09Z, and expiration date -- ::09Z.
  12. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  13. 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'.
  14. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  15. No key escrow sink found. Not writing key {0fe5f8aa---83aa-59df0ed4f0c8} to escrow.
  16. warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  17. No XML encryptor configured. Key {0fe5f8aa---83aa-59df0ed4f0c8} may be persisted to storage in unencrypted form.
  18. info: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[]
  19. Writing data to file '/root/.aspnet/DataProtection-Keys/key-0fe5f8aa-0875-4612-83aa-59df0ed4f0c8.xml'.
  20. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  21. Key cache expiration token triggered by 'CreateNewKey' operation.
  22. dbug: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[]
  23. Reading data from file '/root/.aspnet/DataProtection-Keys/key-0fe5f8aa-0875-4612-83aa-59df0ed4f0c8.xml'.
  24. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[]
  25. Found key {0fe5f8aa---83aa-59df0ed4f0c8}.
  26. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[]
  27. Considering key {0fe5f8aa---83aa-59df0ed4f0c8} with expiration date -- ::09Z as default key.
  28. dbug: Microsoft.AspNetCore.DataProtection.TypeForwardingActivator[]
  29. 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
  30. dbug: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory[]
  31. Using managed symmetric algorithm 'System.Security.Cryptography.Aes'.
  32. dbug: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory[]
  33. Using managed keyed hash algorithm 'System.Security.Cryptography.HMACSHA256'.
  34. dbug: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider[]
  35. Using key {0fe5f8aa---83aa-59df0ed4f0c8} as the default key.
  36. dbug: Microsoft.AspNetCore.DataProtection.Internal.DataProtectionStartupFilter[]
  37. Key ring with default key {0fe5f8aa---83aa-59df0ed4f0c8} was loaded during application startup.
  38. info: Walt.TestMcroServoces.Webapi.Startup[]
  39. infomation
  40. dbug: Microsoft.AspNetCore.Mvc.MvcJsonOptions[]
  41. Compatibility switch AllowInputFormatterExceptionMessages in type MvcJsonOptions is using compatibility value True for version Version_2_1
  42. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  43. Compatibility switch AllowCombiningAuthorizeFilters in type MvcOptions is using compatibility value True for version Version_2_1
  44. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  45. Compatibility switch AllowBindingHeaderValuesToNonStringModelTypes in type MvcOptions is using compatibility value True for version Version_2_1
  46. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  47. Compatibility switch AllowValidatingTopLevelNodes in type MvcOptions is using compatibility value True for version Version_2_1
  48. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  49. Compatibility switch InputFormatterExceptionPolicy in type MvcOptions is using compatibility value MalformedInputExceptions for version Version_2_1
  50. dbug: Microsoft.AspNetCore.Mvc.MvcOptions[]
  51. Compatibility switch SuppressBindingUndefinedValueToEnumType in type MvcOptions is using compatibility value True for version Version_2_1
  52. dbug: Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions[]
  53. Compatibility switch AllowAreas in type RazorPagesOptions is using compatibility value True for version Version_2_1
  54. dbug: Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions[]
  55. Compatibility switch AllowMappingHeadRequestsToGetHandler in type RazorPagesOptions is using compatibility value True for version Version_2_1
  56. dbug: Microsoft.AspNetCore.Mvc.MvcViewOptions[]
  57. 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. java PinYinUtils 拼音工具类

    package com.sicdt.library.core.utils; import java.util.HashSet; import java.util.Set; import net.sou ...

  2. iMX6 yocto平台QT交叉编译环境搭建

    转:https://blog.csdn.net/morixinguan/article/details/79351909 . /opt/fsl-imx-fb/4.9.11-1.0.0/environm ...

  3. Go 外部排序-网络版

    目录结果 main.go package main import ( "NetworkSort/pipeline" "fmt" "os" & ...

  4. mongoDB中批量修改字段

    // 为每一个文章文档新增一个image_count字段,用于记录此文章包含的图片个数 db['test.articles'].find({'title':'wfc test'}).forEach( ...

  5. 【P3522】TEM(单调队列+DP)

    这个题,题目很长,然而亲爱的翻译已经帮你读完题了,一句话题意. 要求不下降的最长,那么这一段肯定满足队首的左区间不大于队尾的右区间,单调队列容易求解. #include<iostream> ...

  6. NLP-特征选择

    文本分类之特征选择 1 研究背景 对于高纬度的分类问题,我们在分类之前一般会进行特征降维,特征降维的技术一般会有特征提取和特征选择.而对于文本分类问题,我们一般使用特征选择方法. 特征提取:PCA.线 ...

  7. Regular Expression Matching,regex,正则表达式匹配,利用动态规划

    问题描述:Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...

  8. c++中的函数对象

    头文件wuyong.h: #pragma once #include<iostream> using namespace std; template<typename T> s ...

  9. hibernate学习(1)

    对象的持久化 狭义的理解,“持久化”仅仅指把对象永久保存到数据库中 广义的理解,“持久化”包括和数据库相关的各种操作 -保存:把对象永久保存到数据库中 -更新:更新数据库中对象的状态 -删除:从数据库 ...

  10. 添加语句<tx:annotation-driven transaction-manager="txManager"/>报错

    在添加<tx:annotation-driven transaction-manager="txManager"/>程序之前,applicationContext.xm ...