【问题标题】:How do you add AWS ASP.NET Core Logging to an ASP.NET Core 2.0 Razor Pages app?如何将 AWS ASP.NET Core 日志记录添加到 ASP.NET Core 2.0 Razor Pages 应用程序?
【发布时间】:2018-10-27 10:34:15
【问题描述】:

我希望使用 nuget 包 AWS.Logger.AspNetCore 在 ASP.NET Core 2.0 Razor Pages 应用程序中实现 ASP.NET Core Logging

项目站点上的示例基于 ASP.NET Core 1.0。

所以例子显示:

在 appsettings.json 文件中:

"AWS.Logging": {
  "Region": "us-east-1",
  "LogGroup": "AspNetCore.WebSample",
  "LogLevel": {
    "Default": "Debug",
    "System": "Information",
    "Microsoft": "Information"
  }
}

在 Startup.cs 中

public Startup(IHostingEnvironment env)
{
    // Read the appsetting.json file for the configuration details
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    // Create a logging provider based on the configuration information passed through the appsettings.json
    loggerFactory.AddAWSProvider(this.Configuration.GetAWSLoggingConfigSection());

    ...

有人可以在 ASP.NET Core 2.0 中显示等效的内容吗? 然后如何将记录器注入到 (.cshtml.cs) 页面后面的 Razor 代码中? (我使用的是 ASP.NET Core Razor 页面,而不是 MVC)

我查看了 Microsoft 的 page on Logging in ASP.NET Core,其中显示了如何添加提供程序,但我不知道如何将其与在 Startup.cs 中添加 AWS Logger 联系起来

【问题讨论】:

    标签: c# amazon-web-services asp.net-core razor-pages


    【解决方案1】:

    剃须刀的方法差不多

    public class PersonModel : PageModel {
       ILogger<PersonModel> Logger { get; set; }
    
        public PersonModel(ILogger<PersonModel> logger)
        {
            this.Logger = logger;
        }
    }
    

    无论如何,它就像确保声明一个私有类型一样简单 然后在构造函数中注入。初创公司 LoggerFactory 正在为您将 AWS 注入记录器。然后,您将 logger 用作 Normal。

    // code to log out to AWS and load the Person.cshtml page
    public void GetAsync(){
      Logger.LogInformation("Welcome to the AWS Logger. You are viewing the home page");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-30
      • 2023-04-08
      • 2020-11-27
      • 2018-03-12
      • 2018-10-29
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      相关资源
      最近更新 更多