3
votes

I am looking to use the nuget package AWS.Logger.AspNetCore to implement ASP.NET Core Logging in an ASP.NET Core 2.0 Razor Pages application.

The examples on the project site are based upon ASP.NET Core 1.0.

So the examples show:

In the appsettings.json file:

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

and in 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());

    ...

Can someone please show the equivalent of this in ASP.NET Core 2.0? And how to then inject the logger into a Razor code behind (.cshtml.cs) page? (I'm using ASP.NET Core Razor Pages, not MVC)

I've looked at Microsoft's page on Logging in ASP.NET Core that shows how to add providers, but I can't figure out how to relate this to adding the AWS Logger within Startup.cs

1

1 Answers

0
votes

The method is more or less the same for razor

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

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

No matter what, its as simple as making sure you declare a private type and then inject within the constructor. The startup, LoggerFactory is injecting AWS into the logger for you. You then use logger as 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");
}