How to set logs to ELK in kibana with authenticati

2020-07-14 01:41发布

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

2条回答
放荡不羁爱自由
2楼-- · 2020-07-14 02:02

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();
查看更多
家丑人穷心不美
3楼-- · 2020-07-14 02:17

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();
查看更多
登录 后发表回答