This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
Basic setup
Michiel van Oudheusden edited this page Mar 31, 2015
·
3 revisions
First make sure to install the package from nuget:
Install-Package serilog.sinks.elasticsearch
This will also install Serilog itself.
You will first need to create a logger. Normally you do this once at startup and place the logger inside your DI container.
var loggerConfig = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200") ){
AutoRegisterTemplate = true,
});
var logger = loggerConfig.CreateLogger();
The above code will register one sink, the Elasticsearch sink, with a reference to the ES server located on the localhost and port 9200. We also set the option **AutoRegisterTemplate ** to true. This will create the correct mapping for us at startup.
Next, we will write some events to Serilog.
logger.Error(new Exception("test"), "An error has occurred.");
logger.Information("The {User} has just executed {Action}.", "username", "actionName");
This will write two events to Serilog which will deliver those to the registered sinks. In this case the Elasticsearch one and your two events will end up in Elasticsearch.