Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set logs to ELK in kibana with authentication using serilog

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!");
like image 488
Shubh Avatar asked Jul 11 '19 16:07

Shubh


1 Answers

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();
like image 159
Bansi Avatar answered Oct 13 '22 12:10

Bansi