Serilog实战

提问

快速上手Serilog步骤

回答

  1. 引用
Serilog.Sinks.Async
Serilog.Sinks.File
Serilog.Sinks.Console

2.配置
appsetting.sjon 加入如下项

 "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },

    "WriteTo:Async": {
      "Name": "Async",
      "Args": {
        "configure": [
          {
            "Name": "File",
            "Args": {
              "path": "logs/log-.txt",
              "rollingInterval": "Day",
              "outputTemplate": "{Timestamp:hh:mm:ss tt} [{Level:u3}] {Message}{NewLine}{Exception}"
            }
          },
          {
            "Name": "Console","Args": {
            "outputTemplate": "{Timestamp:hh:mm:ss tt} [{Level:u3}] {Message}{NewLine}{Exception}"
          }
          }
        ]
      }
    },
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithThreadId"
    ]
  }
  1. 初始化
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var config = new ConfigurationBuilder()
   
    .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
    .Build();

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .ReadFrom.Configuration(config)
    .CreateLogger();
  1. 参考
    https://github.com/serilog/serilog-settings-configuration
posted @   东百牧码人  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2022-09-19 WebAPI如何限制请求Content-Type
2022-09-19 如何单文件独立剪裁部署
点击右上角即可分享
微信分享提示