简体   繁体   English

如何解释 ASP.NET Core 2.1 中的 Serilog 配置?

[英]How do I interpret Serilog configuration in ASP.NET Core 2.1?

For some reason, I find it very hard to understand what's going on with Serilog configuration.出于某种原因,我发现很难理解 Serilog 配置发生了什么。 I have a web api with .NET Core 2.1 and installed serilog.sink.logstash .我有一个带有 .NET Core 2.1 的 web api 并安装了serilog.sink.logstash My startup has:我的创业公司有:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var serverURL = Configuration.GetSection("Logging")["logstashServer"];
    var logger = new LoggerConfiguration()
        .Enrich.FromLogContext()
        .Enrich.WithProperty("Enviroment", env.EnvironmentName)
        .Enrich.WithProperty("ApplicationName", "MyApp")
        .WriteTo.LogstashHttp(serverURL);           

    if (env.IsDevelopment())
    {         
        app.UseDeveloperExceptionPage();
        logger.WriteTo.Console();
    }

    loggerFactory.AddSerilog();

    Log.Logger = logger.CreateLogger();

    app.UseCors("CorsPolicy");
    app.UseMvc();
}

My appsettings has a section:我的 appsettings 有一个部分:

"Logging": {
  "logstashServer": "http://192.168.0.6:8101",
  "IncludeScopes": false,
  "Serilog": {
    "MinimumLevel": {
      "Default": "Error",
      "Override": {
        "Microsoft": "Error",
        "Microsoft.AspNetCore.Hosting": "Error",
        "Microsoft.AspNetCore.Mvc": "Error",
        "System": "Error"
      }
    }
  }
}

No matter what I do, I see both in console and logstash than a lot of unwanted information is logged like:无论我做什么,我都会在控制台和 logstash 中看到很多不需要的信息,例如:

[14:27:54 INF] Executed action method MyApp.Controllers.LoggerController.Post (MyApp), returned result Microsoft.AspNetCore.Mvc.OkResult in 0.0771ms. [14:27:54 INF] 执行操作方法 MyApp.Controllers.LoggerController.Post (MyApp),在 0.0771 毫秒内返回结果 Microsoft.AspNetCore.Mvc.OkResult。
MyApp> [14:27:54 INF] Executing HttpStatusCodeResult, setting HTTP status code 200 MyApp> [14:27:54 INF] 执行 HttpStatusCodeResult,设置 HTTP 状态码 200
MyApp> [14:27:54 INF] Executed action MyApp.Controllers.LoggerController.Post (MyApp) in 2.0855ms MyApp> [14:27:54 INF] 在 2.0855 毫秒内执行操作 MyApp.Controllers.LoggerController.Post (MyApp)

and so on.等等。 Why do I see those since I have minimum level error?为什么我会看到那些,因为我有最小级别的错误?

Although you've added configuration to appsettings.json for overriding the Serilog logging levels, you've not actually passed said configuration into Serilog.尽管您已将配置添加到appsettings.json以覆盖 Serilog 日志记录级别,但您实际上并未将所述配置传递到 Serilog。 At the simplest level, this requires you to install the Serilog.Settings.Configuration nuget package.在最简单的层面上,这需要您安装Serilog.Settings.Configuration nuget 包。 Once you've done that, you can add a call to ReadFrom.Configuration , like so:完成后,您可以添加对ReadFrom.Configuration的调用,如下所示:

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(Configuration.GetSection("Logging"))
    .Enrich.FromLogContext()
    // ...

This is all you need in order to get your configuration into Serilog, but you do have other issues with how you're still using ILoggerFactory inside of Configure ( this changed in ASP.NET Core 2.0 ).这就是将配置导入 Serilog 所需的全部内容,但您在如何在Configure内仍然使用ILoggerFactory确实存在其他问题( 这在 ASP.NET Core 2.0 中有所更改)。 One of the issues this is likely causing for you is that both ASP.NET Core's Console provider and the Serilog Console sink are writing logs.这可能给您造成的问题之一是 ASP.NET Core 的控制台提供程序和 Serilog 控制台接收器都在写入日志。 If you need help with any of that, it's well documented online, but of course you can create additional Stack Overflow questions if absolutely necessary.如果您需要任何方面的帮助,网上都有详细记录,但当然,如果绝对必要,您可以创建其他 Stack Overflow 问题。

Nicholas Blumhardt blogged about the ASP.NET Core 2.0 logging changes - This is a useful read that should help simplify your Serilog + ASP.NET Core experience greatly. Nicholas Blumhardt发表了关于 ASP.NET Core 2.0 日志更改的博客- 这是一篇有用的读物​​,应该有助于极大地简化您的 Serilog + ASP.NET Core 体验。

Looking on how to install, configure and use Serilog on .NET Core 2.1 API project, I found this article very useful.查看如何在.NET Core 2.1 API 项目上安装、配置和使用Serilog ,我发现这篇文章非常有用。

About configuration file, on the Serilog GitHub repository there's a specific page about Serilog.Settings.Configuration package.关于配置文件,在 Serilog GitHub 存储库上有一个关于Serilog.Settings.Configuration包的特定页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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