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 ...
随机推荐
- nuxt中报window is not defined
1.如果是引用插件报错的话,原因是在服务端渲染时找不到window,这样在插件引入位置把ssr设置为false即可. plugins: [ { src: '@/plugins/iview', ssr: ...
- CTF-sql-group by报错注入
本文章主要涉及group by报错注入的原理讲解,如有错误,望指出.(附有目录,如需查看请点右下角) 一.下图为本次文章所使用到 user表,该表所在的数据库为 test 二.首先介绍一下本文章所使用 ...
- Vulnhub - THE PLANETS: EARTH
环境配置 从www.vulnhub.com下载靶机,在VMware中导入,自动分配IP 主机发现 通过对内网主机的扫描,VMware为目标主机 端口扫描 使用nmap对主机进行扫描 发现443端口信息 ...
- Java构造器(构造方法)
类中的构造器也成为构造方法,是在进行创建对象的时候必须调用的,并且构造器有以下两个特点: 1.必须和类名字相同 2.必须没有返回类型也不能写void public class Demo06 { //一 ...
- Java读取批量Excel文件
1.首先基础知识: 原文链接:https://blog.csdn.net/baidu_39298625/article/details/105842725 一 :简介 开发中经常会设计到excel的处 ...
- 【代码分享】用redis+lua实现多个集合取交集并过滤,类似于: select key from set2 where key in (select key from set1) and value>=xxx
redis中的zset结构可以看成一个个包含数值的集合,或者认为是一个关系数据库中用列存储方式存储的一列. 需求 假设我有这样一个数据筛选需求,用SQL表示为: select key from set ...
- Markdown anywhere
最近经常写文章,发现Markdown是一个非常方便的网页排版规范,详见:http://cesiumcn.org/markdown.html | http://cesium.coinidea.com/m ...
- C++多线程之互斥锁和超时锁
#include<iostream> #include<thread> #include<mutex> using namespace std; mutex mu; ...
- python12day
昨日回顾 可迭代对象: 可以更新迭代的实实在在的值. 内部含有'__iter__'方法. str.tuple.dict.set.range 优点:操作方法多,灵活直观 缺点:占用内存. 迭代器: 可以 ...
- C++ DLL注入工具完整源码
先上源码 #include "Inject_Main.h" #include "resource.h" #include <Windows.h> # ...