netcore使用elk日志
1 程序集成
- nuget安装elastic
- nuget安装Serilog.Sinks.Elasticsearch
- startp注入(注意IndexFormat参数,每个程序要单独区分)
public Startup(IConfiguration configuration) { Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Elasticsearch( new ElasticsearchSinkOptions(new Uri("http://123.111.198.108:1130")) { IndexFormat = "logstash-{0:yyyy.MM}", MinimumLogEventLevel = LogEventLevel.Verbose, AutoRegisterTemplate = true, ModifyConnectionSettings = conn => { conn.ServerCertificateValidationCallback((source, certificate, chain, sslPolicyErrors) => true); conn.BasicAuthentication("elastic", "12345"); return conn; } }) .CreateLogger(); Configuration = configuration; }
- 使用方法
using Serilog; namespace ERPWeb.Controllers.Base { public class TestController { private readonly ILogger _logger = Log.Logger; public void Test() { _logger.Debug("debug日志"); _logger.Error("错误日志"); _logger.Information("info日志参数:{@info}", login.basic_info); } } }
2 在线查看
- 日志访问地址:http://123.111.198.108:1131 账号 elastic 密码 12345
- 示例图片
- 对应上边IndexFormat参数
-