![](/img/trans.png)
[英]How to format file log output with asp.net core and Serilog
[英]Serilog Correlate Log Messages package not compatible with asp.net core
I'm using a package "SerilogWeb.Classic" for Correlating log messages, but the package is not compatible with asp.net core, could you please guide me an alternative package, also I'm looking for Serilog metrics, could you please guide me for package 以及
var log = new LoggerConfiguration()
.WriteTo.Console()
.Enrich.WithHttpRequestId() // coming from SerilogWeb.Classic lib
.Enrich.WithUserName()
.CreateLogger();
You can use the default log system of .net core Microsoft.Logging
and in the startup configure the serilog, to do that you can use the package Serilog and Serilog.AspNetCore and the sinks packages to write somewhere, in your case to write into elasticsearch you需要Serilog.Sinks.Elasticsearch 。 你需要配置你的 Program.cs 來配置 serilog, 這里是一個完整的例子。 但基本上你需要添加:
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(configuration["ElasticConfiguration:Uri"]))
{
AutoRegisterTemplate = true,
IndexFormat = $"{Assembly.GetExecutingAssembly().GetName().Name.ToLower()}-{DateTime.UtcNow:yyyy-MM}"
})
.Enrich.WithProperty("Environment", environment)
.ReadFrom.Configuration(configuration)
.CreateLogger();
並在您的構建器中添加.UseSerilog()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.