惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - LoveCoder

一个HTML小工具,可以显示你在网页上触摸点击的轨迹、位置、按压时长以及你触发的所有JS事件 SQL Server查询一个很大字段varchar字段的字符串被截断问题 sql server 每个表占用大小查询【转】 电商ERP系统源码出售 kubernetes镜像拉取失败解决方法 ErrImagePull 淘宝虚拟商品自动发货接口 如何排查线上w3wp.exe CPU高的问题,使用到了WinDbg、Visual studio来分析IIS进程池的.dmp文件 sql server 将数据库表里面的数据,转为insert语句,方便小批量转移数据 淘宝库存更新修改接口 淘宝开放平台上货接口,淘宝商品发布接口 C# 模拟http请求出现 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作[windows服务器] 淘宝订单信息获取接口API,淘宝打单发货接口 使用win-acme在windows+iis服务器下配置自动续期SSL证书【转】 开发Android应用程序,在Android10的系统上提示网络出错? android 反编译APK取源代码。 .net7(.net core) 依赖注入:从 AddSingleton 注册的类里面访问 AddScoped 的问题 淘宝订单信息获取接口API,淘宝打单发货接口 淘宝订单信息获取接口,淘宝订单信息获取API win10远程登录的账号密码
在.net core使用Serilog,只要简单的三步
LoveCoder · 2023-09-22 · via 博客园 - LoveCoder

第一步:在项目上用nuget安装 Serilog.AspNetCore 最新的稳定版即可 ,安装这个会把其他需要的包都给包含着

第二步:修改 Program.cs 的 CreateHostBuilder 方法,加入 .UseSerilog(),修改后的 CreateHostBuilder  方法如下

  public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                }).UseSerilog((hostingContext, loggerConfiguration) =>
                {
                    loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
                });
上面是.net core 5.0以及之前版本的 ,.net core 6.0版本是没有.CreateHostBuilder 方法,可以在 Program.cs中的 var builder = WebApplication.CreateBuilder(args); 后面这样写
var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((hostingContext, loggerConfiguration) =>
{
loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
});

第三步:修改 appsettings.json,加入如下节点

"Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
    // 日志输出级别
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        // 日志调用类命名空间如果以 Microsoft 开头,覆盖日志输出最小级别为 Information
        "Microsoft": "Warning",
        "Microsoft.Hosting": "Information",
        "System": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "File",
        "Args": { "path": "Logs/log.txt" }
      }
    ]
  }

接下来就可以调用了

调用可以如下:

   Log.Error("test");

或者在构造函数中取

 private readonly ILogger<QiMenController> _logger;
        public QiMenController(ILogger<QiMenController> logger) 
        {
            _logger = logger;
        }

 json里面如何配置日志最小等级、输出到什么介质等,自己搜资料吧,我也是搜的。