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

How to set logs to ELK in kibana with authentication using 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 want to use with log4net then

Step1: Install this NuGet package "log4net.Elasticsearch"

Step2: Add this "[assembly: log4net.Config.XmlConfigurator(Watch = true)]" in AssemblyInfo.cs

Step3: Add these things in web.config

<configSections>   <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /></configSections><runtime>   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">       <dependentAssembly>           <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />           <bindingRedirect oldVersion="0.0.0.0-2.0.8.0" newVersion="2.5.0" />       </dependentAssembly>   </assemblyBinding></runtime><log4net>   <appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch">       <connectionString value="Scheme=http;Server=your_IP;Index=index_name;Port=9200;User=your_Username;Pwd=your_Password;rolling=true;" />  <!-- make sure index start with small letter -->   </appender>   <root>       <level value="ALL" />       <appender-ref ref="ElasticSearchAppender" />   </root></log4net>

Step4: Log events where you want by adding

using log4net;private static readonly ILog _log = LogManager.GetLogger(typeof(your_mainpageclass));_log.Error("your message ", ex);_log.Logger.Repository.Shutdown();