• ABP:内置logger and 第三方库serilog 之间的关系


    今天在看项目的日志记录功能,比较疑惑为什么项目中有的地方使用 Logger< T > _logger 然后又在资源文件下看见日志的第三方库 Serilog

    解答:

    ASP.NET Core Build-in Logging
    ASP.NET Core 提供了 Logging 的抽象接口, third party 都会依据抽象来做实现. ASP.NET Core 自己也实现了一套简单的 log, 它只能 log to console. 不能 log to file.

    只能输出到控制台,但是不能输出到文件

    所以绝大部分项目都会搭配一个 third party library, 比如 Serilog.

    Serilog 的 config 不是通过 appsetting 设置的,

    如果想用 appsetting 来管理可以另外安装一个 DLL

    dotnet add package Serilog.Settings.Configuration 

    appsetting 里的 log config 是给 ASP.NET Core build-in log 的, Serilog 不会读取它来用.

    所以第0步:装Serilog

    dotnet add package Serilog.AspNetCore

    第1步:新建一个文件覆写内置的log功能

    1. using System;
    2. using System.IO;
    3. using Serilog;
    4. using Serilog.Events;
    5. namespace UIH.RT.HRuiJin.Log
    6. {
    7. public class SerilogByMySelf : ILogger // 看吧serilog要接盘内置的log
    8. {
    9. private readonly Serilog.Core.Logger _logger;
    10. private const string logFilePath = "文件路径.txt";
    11. protected string logFileFolder = "日志文件夹的名字";
    12. protected string date = DateTime.Today.ToString("yyyyMMdd");
    13. protected virtual string LogFilePath => Path.Combine(logFolderName, date, logFilePath);
    14. public SerilogByMySelf()
    15. {
    16. _logger = new LoggerConfiguration()
    17. .MinimumLevel.Verbose()
    18. .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    19. .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
    20. .WriteTo.Async("写你的文件路径")
    21. .CreateLogger();
    22. }
    23. // 接盘之后也可以加自定义操作
    24. public void Warning(object obj)
    25. {
    26. _logger.Warning(obj.ToString());
    27. }
    28. }
    29. }

    第2步:go to the  program.cs 去程序的入口配置

    解决答案REF:c# - Use Serilog with Microsoft.Extensions.Logging.ILogger - Stack Overflow

    1. public class Program
    2. {
    3. public static int Main(string[] args)
    4. {
    5. SeriLog.Logger = new SerilogFucker();
    6. internal static IHostBuilder CreateHostBuilder(string[] args) =>
    7. Host.CreateDefaultBuilder(args)
    8. .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup()})
    9. .UseSerilog(); // 这一行最重要!!!
    10. }

    关于内置logger的资料 :ASP.NET Core 源码学习之 Logging[1]:Introduction - 雨夜朦胧 - 博客园

    现在面临的问题:

    1. 对于微服务的调用记录,存储方式如何? 文件 or 数据库

    2. 如果是文件,改代码最方便的方法是新建一个 “Api_History” 文件,在调用proxy api时候对每一个方法手动地添加 _logger.info( ) 

    3. 或者适配器模式,抽象出一个适配器类 or 桥接类 or super class虚方法?

    4.或者ABP的审计更合适?

    2022/12/1

    得,问了问TeamLeader, 方法函数内纯手打就行了。直接干就完事了

  • 相关阅读:
    C++ 与复合数据类型:透过类理解结构体
    分享一下便利店怎么做微信小程序
    RS485通讯方式-详解
    基于 CoreDNS 和 K8s 构建云原生场景下的企业级 DNS
    AnolisOS 外传二: 在oracle 云安装并使用anolis系统
    集成服饰SCM系统领猫连通更多应用
    数据结构与算法:数据结构基础
    如何做好工作汇报?这些要点要注意
    开发需要了解的23种设计模式 | 导言
    TCP 报文首部的 6 个标记位
  • 原文地址:https://blog.csdn.net/dongnihao/article/details/128132700