我想将日志发送到ElasticSearch并在Kibana中查看它们。但由于某些奇怪的原因,它无法正常工作:项目正在执行某些操作,但是在Kibana中没有任何内容。我下载并尝试了与此主题相关的超过10个项目,尽管它们被介绍为有效的样本,但它们都没有完成任务:在Kibana中没有任何内容!
这里我发布其中一个示例项目的源代码:
这里我发布其中一个示例项目的源代码:
public class Startup
{
public Startup(IConfiguration configuration)
{
// Create Serilog Elasticsearch logger
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Information() //debug
.WriteTo.Elasticsearch().WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
{
MinimumLogEventLevel = LogEventLevel.Verbose,
AutoRegisterTemplate = true
})
.CreateLogger();
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
这是控制器(Controller):
public IActionResult Index()
{
_logger.LogInformation("Hello world!");
return View();
}
public IActionResult About()
{
var elasticUri = new Uri("http://localhost:9200/cmstest/obj");
var loggerConfig = new LoggerConfiguration()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(elasticUri)
{
AutoRegisterTemplate = true,
MinimumLogEventLevel = Serilog.Events.LogEventLevel.Verbose
});
var logger = loggerConfig.CreateLogger();
logger.Error("Hello world");
logger.Information("Hello world");
logger.Warning("Hello world");
return View();
}
public IActionResult Contact()
{
_logger.LogInformation("Hello world");
var logger = new LoggerConfiguration()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(
new Uri("http://localhost:9200"))
{
AutoRegisterTemplate = true
}).CreateLogger();
logger.Information("Hello world");
return View();
}
我也发现了使用log4net的示例 - 也没有生效
Serilog.Debugging.SelfLog.Enable(message => Console.WriteLine(message));
以将Serilog错误输出到控制台。默认情况下,ES Sink会将任何问题报告给SelfLog。https://github.com/serilog/serilog-sinks-elasticsearch#handling-errors - Justus Burger