简体   繁体   中英

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!");

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM