【发布时间】:2018-02-22 11:30:36
【问题描述】:
我正在尝试将日志发送到 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?}");
});
}
}
这是控制器:
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 和 Elastic Stack 的示例:github.com/FantasticFiasco/serilog-sinks-http-elastic-stack
-
尝试添加
Serilog.Debugging.SelfLog.Enable(message => Console.WriteLine(message));以将 Serilog 错误打印到控制台。默认情况下,ES sink 会向 SelfLog 报告任何问题。 github.com/serilog/serilog-sinks-elasticsearch#handling-errors -
你解决了吗?我遇到了类似的问题
-
谢谢@ChristianDavén - 是的,设置好了,但它仍然无法正常工作。文件日志记录+控制台日志记录都没有问题......
标签: c# elasticsearch logging kibana serilog