C# 记录(通过Serilog)到ElasticSearch不起作用
我正在尝试将日志发送到ElasticSearch,并在Kibana中查看它们。出于某种奇怪的原因,它不起作用:这些项目正在做一些事情,但在基巴纳却什么都没有。我下载并尝试了10多个与此主题相关的项目,虽然它们被介绍为工作示例,但没有一个成功:Kibana中没有任何内容 我在这里发布其中一个的源代码:C# 记录(通过Serilog)到ElasticSearch不起作用,c#,
elasticsearch,logging,kibana,serilog,C#,
elasticsearch,Logging,Kibana,Serilog,我正在尝试将日志发送到ElasticSearch,并在Kibana中查看它们。出于某种奇怪的原因,它不起作用:这些项目正在做一些事情,但在基巴纳却什么都没有。我下载并尝试了10多个与此主题相关的项目,虽然它们被介绍为工作示例,但没有一个成功:Kibana中没有任何内容 我在这里发布其中一个的源代码: public class Startup { public Startup(IConfiguration configuration) { // Create Ser
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?}");
});
}
}
这是控制器:
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();
}
PS
我还发现log4net的示例也不起作用如果您询问Elasticsearch sink的开发人员,他们应该能够帮助您。如果您正在运行Logstash,您可以尝试Serilog HTTP插件,这里还有一个显示Serilog和弹性堆栈的示例:尝试添加
Serilog.debug.SelfLog.Enable(message=>Console.WriteLine(message))
将Serilog错误打印到控制台。默认情况下,ES接收器将向SelfLog报告任何问题。你设法解决了吗?我也遇到了类似的问题。谢谢@ChristianDavén-是的,设置好了,但它仍然不起作用。文件日志记录和控制台日志记录都没有问题。。。。