如何将AWS ASP.NET Core Logging添加到ASP.NET Core 2.0 Razor Pages应用程序中?

3
我想使用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中的等效代码吗? 然后如何将日志记录器注入到Razor代码后面的(.cshtml.cs)页面中?(我正在使用ASP.NET Core Razor Pages,而不是MVC)
我已经查看了微软关于ASP.NET Core日志记录的页面,其中显示了如何添加提供程序,但我无法弄清楚如何将AWS Logger添加到Startup.cs中。
1个回答

0

对于Razor,方法或多或少是相同的。

public class PersonModel : PageModel {
   ILogger<PersonModel> Logger { get; set; }

    public PersonModel(ILogger<PersonModel> logger)
    {
        this.Logger = logger;
    }
}

无论如何,最简单的方法是确保你声明一个私有类型并在构造函数中注入。启动LoggerFactory会为日志记录器注入AWS。然后你可以像平常一样使用日志记录器。
// 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");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接