aspnetcore 使用serilog日志
首先建个aspnetcorewebapi6.0的项目

安装组件:


Seq — centralized structured logs for .NET, Java, Node.js (datalust.co)

using Serilog;
using Serilog.Events; // Setup serilog in a two-step process. First, we configure basic logging
// to be able to log errors during ASP.NET Core startup. Later, we read
// log settings from appsettings.json. Read more at
// https://github.com/serilog/serilog-aspnetcore#two-stage-initialization.
// General information about serilog can be found at
// https://serilog.net/
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger(); try
{
Log.Information("Starting the web host");
var builder = WebApplication.CreateBuilder(args);
// Full setup of serilog. We read log settings from appsettings.json
builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext());
// Add services to the container. builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline.
app.UseSerilogRequestLogging(configure =>
{
configure.MessageTemplate = "HTTP {RequestMethod} {RequestPath} ({UserId}) responded {StatusCode} in {Elapsed:0.0000}ms";
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
} app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
}
catch
(Exception ex)
{
Log.Fatal(ex, "Host terminated unexpexctedly");
}
finally
{
Log.CloseAndFlush();
}
{
//"Logging": {
// "LogLevel": {
// "Default": "Information",
// "Microsoft.AspNetCore": "Warning"
// }
//},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Seq" ],
"MinimumLevel": "Information",
// Where do we want to write our logs to? Choose from a large number of sinks:
// https://github.com/serilog/serilog/wiki/Provided-Sinks.
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": { "path": "Logs/log.txt" }
},
{
"Name": "Seq",
"Args": { "serverUrl": "http://localhost:8888" }
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "AspNetCoreSerilogDemo"
}
},
"AllowedHosts": "*"
}
运行结果如下,已替换系统自带information:


请求跟踪分析:
using Microsoft.AspNetCore.Mvc; namespace AspNetCoreSerilogDemo.Controllers
{
[ApiController]
[Route("[controller]")]
public class SeriLogDemoController : ControllerBase
{ private readonly ILogger<SeriLogDemoController> _logger; public SeriLogDemoController(ILogger<SeriLogDemoController> logger)
{
_logger = logger;
} [HttpGet]
public string String()
{
_logger.LogInformation("this is serilog...");
return "Suscess";
} }
}

配置文件里面输出路径有"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Seq" ],所以同样会输出到日志文件中,指定路径和文件名:


更多更详细功能参考:
Serilog — simple .NET logging with fully-structured events
Seq — centralized structured logs for .NET, Java, Node.js (datalust.co)
示例代码:
exercise/AspNetCoreSerilogDemo at master · liuzhixin405/exercise (github.com)
aspnetcore 使用serilog日志的更多相关文章
- ASP.NET Core MVC之Serilog日志处理,你了解多少?
前言 本节我们来看看ASP.NET Core MVC中比较常用的功能,对于导入和导出目前仍在探索中,项目需要自定义列合并,所以事先探索了如何在ASP.NET Core MVC进行导入.导出,更高级的内 ...
- .NET Worker Service 添加 Serilog 日志记录
前面我们了解了 .NET Worker Service 的入门知识[1] 和 如何优雅退出 Worker Service [2],今天我们接着介绍一下如何为 Worker Service 添加 Ser ...
- 十八、.net core(.NET 6)搭建ElasticSearch(ES)系列之使用Logstash通过Rabbitmq接收Serilog日志到ES
使用Logstash通过Rabbitmq接收Serilog日志到ES 首先,要部署logstash 为了与前面的ElasticSearch版本保持一致,此处Logstash下载的版本也是7.13.1, ...
- AspNetCore 使用NLog日志,NLog是基于.NET平台开的类库!(又一神器)
NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码. NLog是一个简单灵活的.NET日志记录类库.通过使用NLog,我们可以在任何一种.NET语言中 ...
- EL+Serilog日志
简介 Elasticsearch 是一个实时的分布式搜索分析引擎,它能让你以前所未有的速度和规模,去探索你的数据. 它被用作全文检索.结构化搜索.分析以及这三个功能的组合: 安装 Elasticsea ...
- ASP.NET Core 集成测试中通过 Serilog 向控制台输出日志
日志是程序员的雷达,不仅在生产环境中需要,在集成测试环境中也需要,可以在持续集成失败后帮助定位问题.与生产环境不同,在集成测试环境中使用控制台输出日志更方便,这样可以通过持续集成 runner 执行 ...
- TraceID在AspNETCore日志排障中的应用
前言 .NetCore日志,相信大家多少都接触过,博客园有关 ① AspNetCore依赖注入第三方日志组件 ②第三方日志组件Nlog,Serilog 应用方法的博文层出不穷. 结合程序的部署结构 ...
- 如何利用Serilog的RequestLogging来精简ASP.NET Core的日志输出
这是该系列的第一篇文章:在ASP.NET Core 3.0中使用Serilog.AspNetCore. 第1部分-使用Serilog RequestLogging来简化ASP.NET Core的日志输 ...
- 基于.NetCore3.1系列 —— 日志记录之初识Serilog
一.前言 对内置日志系统的整体实现进行了介绍之后,可以通过使用内置记录器来实现日志的输出路径.而在实际项目开发中,使用第三方日志框架(如: Log4Net.NLog.Loggr.Serilog.Sen ...
随机推荐
- 利用Javaweb应用中六种属性范围,来理解Servlet的并发问题
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6513748225550189060/ Web应用中有六种属性范围: (1) 局部变量 (2) 实例变量 (3) 类 ...
- Android官方文档翻译 十五 3.3Supporting Different Platform Versions
Supporting Different Platform Versions 支持不同的平台版本 This lesson teaches you to 这节课教给你 Specify Minimum a ...
- 🏆【Alibaba中间件技术系列】「RocketMQ技术专题」Broker服务端自动创建topic的原理分析和问题要点指南
前提背景 使用RocketMQ进行发消息时,一般我们是必须要指定topic,此外topic必须要提前建立,但是topic的创建(自动或者手动方式)的设置有一个开关autoCreateTopicEnab ...
- 刷机错误ERROR:STATUS_BROM_CMD__FAIL
ERROR:STATUS_BROM_CMD_STARTCMD_FAIL window驱动没有安装好,几乎所有安装包都有问题,很难成功,成功了也很慢.这是因为之前检测到且烧写错误,然后上一次的驱动错误连 ...
- How to mount Windows network disk in WSL
Backgroud Mount samba directly in wsl like linux is difficult Password for root@//filesystem.domain/ ...
- vue学习5-js表达式
三目运算符 <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <m ...
- SpringCloud之使用Zookeeper作为注册中心
SpringCloud之使用Zookeeper作为注册中心 linux安装zookeeper 安装zookeeper 关闭linux防火墙 启动zookeeper 1 创建项目导入依赖和配置文件 &l ...
- gorm中的高级查询
智能选择字段 GORM 允许通过 Select 方法选择特定的字段,如果您在应用程序中经常使用此功能,你也可以定义一个较小的结构体,以实现调用 API 时自动选择特定的字段,例如: type User ...
- 『无为则无心』Python函数 — 39、Python中异常的传播
目录 1.异常的传播 2.如何处理异常 1.异常的传播 当在函数中出现异常时,如果在函数中对异常进行了处理,则异常不会再继续传播.如果函数中没有对异常进行处理,则异常会继续向函数调用者传播.如果函数调 ...
- hive 常用日期格式转换
固定日期转换成时间戳select unix_timestamp('2016-08-16','yyyy-MM-dd') --1471276800select unix_timestamp('201608 ...