代码之家  ›  专栏  ›  技术社区  ›  wpfwannabe

谷歌。云诊断。AspNetCore3调试级别不工作

  •  0
  • wpfwannabe  · 技术社区  · 4 年前

    考虑一个向导生成的ASP。NET核心项目(NET 6)。添加一个 Google.Cloud.Diagnostics.AspNetCore3 NuGet包和 services.AddGoogleDiagnosticsForAspNetCore() 到 Startup.cs 允许 GOOGLE_APPLICATION_CREDENTIALS 环境变量指向服务帐户JSON的路径。

    在应用程序的某个位置(例如控制器)添加以下内容:

    _logger.LogDebug("Nope");
    _logger.LogInformation("Yeah");
    

    谷歌云日志浏览器只显示“是”(没有特定的过滤器)。我的 appsettings.json 看起来像:

    "Logging": {
      "LogLevel": {
        "Default": "Debug",
        "System": "Information",
        "Microsoft": "Information"
      }
    }
    

    据我所知 "Default": "Debug" 应该在缺少更具体配置的任何地方工作。

    为什么我没有看到“否”被记录?有什么明显我遗漏的吗?值得一提的是,VisualStudio调试输出和控制台输出都显示出预期的“否/是”。

    0 回复  |  直到 4 年前
        1
  •  4
  •   Yılmaz Durmaz    4 年前

    简短回答: Google.Cloud.Diagnostics.AspNetCore3 不使用 appsettings.json (至少目前如此)并且必须显式设置日志级别。

    现在来看长答案和之后的工作代码。

    要将谷歌诊断添加到我们的项目中,我们有3个重载 ...AddGoogleDiagnosticsForAspNetCore(...) 可用,而且 ...AddGoogle(...) 只是为了使用我们需要的服务,比如日志记录服务。( ... 开头的更改取决于dotnet版本,末尾的示例)。

    1-在GCP环境中, ...AddGoogleDiagnosticsForAspNetCore() 签名用于设置诊断程序的默认值。从GCP获取服务详细信息。

    2-在GCP环境中, ...AddGoogleDiagnosticsForAspNetCore( AspNetCoreTraceOptions, LoggingServiceOptions, ErrorReportingServiceOptions ) 签名我们可以设置3种类型的选项:AspNet跟踪、日志记录服务和错误报告服务。

    • 对于这个用例,如果我们只想记录服务,我们可以使用位置参数 (null,new LoggingServiceOptions{...},null) (不需要最后一个null)或命名参数 (loggingOptions: new LoggingServiceOptions{...})
    • 有很多需要设置 LoggingServicesOptions{...} 但仅为了日志级别的目的,以下内容就足够了: new LoggingServiceOptions{ Options = LoggingOptions.Create(logLevel: LogLevel.Debug) } 。

    现在我们来谈谈重要的一点。尽管文档隐含地涵盖了足够多的内容,但并没有明确表示此用例将直接设置选项,而不是服务。

    3-虽然没有明确说明,但此用法适用于GCP之外的情况或GCP无法正确设置的情况(不确定如何设置!?) AddGoogleDiagnosticsForAspNetCore( projectId, serviceName, serviceVersion, TraceOptions, LoggingOptions, ErrorReportingOptions ) 。起初,这可能看起来类似于第二个签名,但它没有设置服务选项。

    • 当一个人看到 Project ID was not provided and could not be autodetected 第一个或第二个签名的消息,他们必须将其作为一个参数提供,该参数将立即切换函数以使用第三个签名。
    • 在这种情况下,如果我们只想要日志记录服务,则必须以 (projectId,null,null,null,LoggingOptions...,null) 对于位置参数(不需要最后一个null)或 (projectId:"some ID",loggingOptions: LoggingOptions...) 用于命名参数
    • LoggingOptions... 就是简单的 LoggingOptions.Create(logLevel: LogLevel.Debug) 以设置日志级别。

    4-除了在将谷歌诊断添加到服务时添加这些详细信息外,我们还可以在设置配置时添加日志记录选项: ...AddGoogle( LoggingServiceOptions{...} ) 。但在这种使用中,我们需要在其中提供项目Id; new LoggingServiceOptions{ ProjectId = "some ID", Options = LoggingOptions.Create(logLevel: LogLevel.Debug) }


    填写 。。。

    dotnet6开始使用新的顶级语句。因此,我们需要遵循以下步骤。

    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddGoogleDiagnosticsForAspNetCore(
     projectId: "some ID",
     loggingOptions:  LoggingOptions.Create(logLevel: LogLevel.Debug)
    );
    
    // or
    var builder = WebApplication.CreateBuilder(args);
    builder.Logging.AddGoogle(
      new LoggingServiceOptions { 
        ProjectId = "some ID",
        Options=LoggingOptions.Create(logLevel:LogLevel.Debug)
      }
    );
    

    由于OP提到使用 Startup.cs ,该项目使用旧样式,因此这些是必需的部分。

    // inside ConfigureServices
    services.AddGoogleDiagnosticsForAspNetCore(
     projectId: "some ID",
     loggingOptions:  LoggingOptions.Create(logLevel: LogLevel.Debug)
    );
    
    // or
    // before using "UseStartup"
    .ConfigureLogging(
      builder => builder.AddGoogle(
        new LoggingServiceOptions { 
          ProjectId = "some ID",
          Options=LoggingOptions.Create(logLevel:LogLevel.Debug)
        }
      )
    )
    

    额外的

    我们可以从配置文件中读取(顶级格式)

    var builder = WebApplication.CreateBuilder(args);
    var config = builder.Configuration;
    builder.Services.AddGoogleDiagnosticsForAspNetCore(
      projectId:config["GCP:ID"],
      loggingOptions:  LoggingOptions.Create(
         logLevel: Enum.Parse<LogLevel>(config["GCP:Logging:LogLevel:Default"]
    )));
    

    并在中添加一个GCP部分 appsettings.json

      "GCP":{
        "ID":"some ID",
        "Logging":{
          "LogLevel":{
            "Default":"Debug"
          }
        }
      }
    
        2
  •  3
  •   Mr Patience    4 年前

    我已经下载了上面提到的包(它是开源的),并检查了默认日志选项的创建:

    enter image description here

    正如您可能看到的默认值 logLevel 是 Information 。

    在执行过程中,没有迹象表明 level 从配置-它只是从您可以在代码中指定的选项中传递的:

    初始调用: enter image description here

    服务提供商注册: enter image description here

    日志记录提供程序和选项的创建: enter image description here

    选项的创建(第一张图片) enter image description here

    以及记录器的创建(可能由ASP.NET内部调用) enter image description here

    简单的答案是 谷歌软件包没有从中读取任何内容 appsettings.json 默认情况下。

    您可以使用设置日志记录级别 LoggingOptions :
    (为简洁起见,省略了其他选项)

    builder.Services.AddGoogleDiagnosticsForAspNetCore(loggingOptions: new Google.Cloud.Diagnostics.Common.LoggingServiceOptions
    {
        // ... Other required options, e.g. projectId
        Options = Google.Cloud.Diagnostics.Common
            .LoggingOptions.Create(logLevel: LogLevel.Debug
            // ... Other necessary options 
            ),
    });
    
    推荐文章