如何使用带有身份验证的serilog在kibana中将日志设置为ELK

时间:2019-07-11 16:12:57

标签: c# asp.net elasticsearch serilog

我已经设置了示例代码,但无法使用serilog登录到带有验证的kibana。 在这里,我已附加我的代码,请对其进行更正。

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

4 个答案:

答案 0 :(得分:5)

第一步:安装此NuGet软件包“ Serilog.Sinks.Elasticsearch”

第二步:将其添加到App.config或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>

第3步:在main()的program.cs中或Application_Start()的Global.asax中将其添加

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:通过添加将日志记录到所需位置

 using Serilog;
 Log.Error("Your_Message", ex);
 Log.CloseAndFlush();

答案 1 :(得分:2)

如果要与log4net一起使用,则

第一步:安装此NuGet软件包“ log4net.Elasticsearch”

Step2:在AssemblyInfo.cs中添加此“ [assembly:log4net.Config.XmlConfigurator(Watch = true)]”

Step3:在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:通过添加将日志记录到所需位置

using log4net;
private static readonly ILog _log = LogManager.GetLogger(typeof(your_mainpageclass));

_log.Error("your message ", ex);
_log.Logger.Repository.Shutdown();

答案 2 :(得分:0)

第一步:安装此NuGet软件包“ Serilog.Sinks.Elasticsearch”

第二步:将其添加到App.config或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>

第3步:在main()的program.cs中或Application_Start()的Global.asax中将其添加

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:通过添加将日志记录到所需位置

using Serilog;
Log.Error("Your_Message", ex);
Log.CloseAndFlush();

答案 3 :(得分:0)

如果要与log4net一起使用,则

第一步:安装此NuGet软件包“ log4net.Elasticsearch”

Step2:在AssemblyInfo.cs中添加此“ [assembly:log4net.Config.XmlConfigurator(Watch = true)]”

Step3:在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:通过添加将日志记录到所需位置

using log4net;
private static readonly ILog _log = LogManager.GetLogger(typeof(your_mainpageclass));

_log.Error("your message ", ex);
_log.Logger.Repository.Shutdown();