I have set an example of my code but I'm not able to logs in kibana with authentication using serilog. Here, I have attached my code please correct it.
Log.Logger = new LoggerConfiguration()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("myurl:9200"))
{
IndexFormat = "ChargeMasterlog-{yyyy.MM.dd}",
ModifyConnectionSettings = x => x.BasicAuthentication("username", "password"),
}).CreateLogger();
Log.Information("Hello, Serilog!");
Step1: Install this NuGet package "Serilog.Sinks.Elasticsearch"
Step2: Add this in App.config or Web.config
<appSettings>
<add key="elasticsearchURL" value="your_URL" />
<add key="elasticsearchuserName" value="your_Username" />
<add key="elasticsearchpassword" value="your_Password" />
<add key="elasticsearchIndex" value="indexname-{0:yyyy.MM.dd}" /> <!-- make sure index start with small letter -->
</appSettings>
Step3: Add this in program.cs in main() OR Global.asax in Application_Start()
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(ConfigurationManager.AppSettings["elasticsearchURL"]))
{
AutoRegisterTemplate = true,
ModifyConnectionSettings = x => x.BasicAuthentication(ConfigurationManager.AppSettings["elasticsearchuserName"], ConfigurationManager.AppSettings["elasticsearchpassword"]),
IndexFormat = ConfigurationManager.AppSettings["elasticsearchIndex"]
})
.CreateLogger();
Step4: Log events where you want by adding
using Serilog;
Log.Error("Your_Message", ex);
Log.CloseAndFlush();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With