Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh AWS.Logger.AspNetCore #90

Merged
merged 12 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions AWS.Logger.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProgrammaticConfigurationEx
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleSample", "samples\AspNetCore\ConsoleSample\ConsoleSample.csproj", "{49860DDF-0A02-4D51-BF75-15B336455661}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSample", "samples\AspNetCore\WebSample\WebSample.csproj", "{78B3E877-3E58-4647-B928-5E5852295362}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigExample", "samples\Log4net\ConfigExample\ConfigExample.csproj", "{34419D16-C17E-48FC-AEFB-D56A77C6419B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProgrammaticConfigurationExample", "samples\Log4net\ProgrammaticConfigurationExample\ProgrammaticConfigurationExample.csproj", "{3A725E5C-A78B-4724-9934-AD2D96A158C6}"
Expand Down Expand Up @@ -61,6 +59,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AWS.Logger.SeriLog", "src\A
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicAWSCredentialsConfigurationExample", "samples\Log4net\BasicAWSCredentialsConfigurationExample\BasicAWSCredentialsConfigurationExample.csproj", "{7249C7F3-A0EF-4940-9931-3096C87E009E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebSample", "samples\AspNetCore\WebSample\WebSample.csproj", "{95F48BA5-D01C-47F1-9D66-3A6D122A314E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -87,10 +87,6 @@ Global
{49860DDF-0A02-4D51-BF75-15B336455661}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49860DDF-0A02-4D51-BF75-15B336455661}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49860DDF-0A02-4D51-BF75-15B336455661}.Release|Any CPU.Build.0 = Release|Any CPU
{78B3E877-3E58-4647-B928-5E5852295362}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78B3E877-3E58-4647-B928-5E5852295362}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78B3E877-3E58-4647-B928-5E5852295362}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78B3E877-3E58-4647-B928-5E5852295362}.Release|Any CPU.Build.0 = Release|Any CPU
{34419D16-C17E-48FC-AEFB-D56A77C6419B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34419D16-C17E-48FC-AEFB-D56A77C6419B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34419D16-C17E-48FC-AEFB-D56A77C6419B}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -147,6 +143,10 @@ Global
{7249C7F3-A0EF-4940-9931-3096C87E009E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7249C7F3-A0EF-4940-9931-3096C87E009E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7249C7F3-A0EF-4940-9931-3096C87E009E}.Release|Any CPU.Build.0 = Release|Any CPU
{95F48BA5-D01C-47F1-9D66-3A6D122A314E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95F48BA5-D01C-47F1-9D66-3A6D122A314E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95F48BA5-D01C-47F1-9D66-3A6D122A314E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95F48BA5-D01C-47F1-9D66-3A6D122A314E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -160,7 +160,6 @@ Global
{71373EC1-B7CD-41E5-9AEE-2D58269079A2} = {7A064171-6FA7-44CC-9819-2C383A3195D9}
{BEC56974-2CAF-4FC0-848F-312FF153174F} = {7A064171-6FA7-44CC-9819-2C383A3195D9}
{49860DDF-0A02-4D51-BF75-15B336455661} = {207FA88A-9F32-45A0-84AC-838383875A55}
{78B3E877-3E58-4647-B928-5E5852295362} = {207FA88A-9F32-45A0-84AC-838383875A55}
{34419D16-C17E-48FC-AEFB-D56A77C6419B} = {D28DFB15-FA6F-42EE-890C-E6300A187CCA}
{3A725E5C-A78B-4724-9934-AD2D96A158C6} = {D28DFB15-FA6F-42EE-890C-E6300A187CCA}
{F7986868-9D7E-4DF6-A57E-E79E38AD166D} = {5646565D-E0F8-4EB3-AE6A-6C288B831C7A}
Expand All @@ -175,6 +174,7 @@ Global
{D18AD3E0-9465-4393-AEDB-F2E75DEE839C} = {5646565D-E0F8-4EB3-AE6A-6C288B831C7A}
{916D3A6A-3713-48FB-8978-1440CD02F782} = {E3018A04-6209-4500-B8F2-706514407F90}
{7249C7F3-A0EF-4940-9931-3096C87E009E} = {D28DFB15-FA6F-42EE-890C-E6300A187CCA}
{95F48BA5-D01C-47F1-9D66-3A6D122A314E} = {207FA88A-9F32-45A0-84AC-838383875A55}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {46F50BDA-08A5-4A81-9CA9-F0B732C341A7}
Expand Down
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,26 @@ Checkout the [Log4net samples](/samples/Log4net) for examples of how you can use
ASP.NET Core introduced a new [logging framework](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging) that has providers configured to send logs to destinations.
The AWS.Logger.AspNetCore NuGet package provides a log provider which adds CloudWatch Logs as a destination for the logs.

**Note:** Starting with version 2.0.0 of AWS.Logger.AspNetCore this library targets netstandard2.0 and the dependencies have been
upgraded to the ASP.NET Core 2.1 versions. For older versions of .NET Core, which Microsoft has made end of life, use versions before 2.0.0.

The [WebSample](/samples/AspNetCore/WebSample) in this repository demonstrates how to configure
this provider.

The configuration is setup in the [appsettings.json](/samples/AspNetCore/WebSample/appsettings.json) file
The configuration is setup in the [appsettings.json](/samples/AspNetCore/WebSample/appsettings.json) file. In versions before 2.0.0 the `AWS.Logging`
was used as the configuration section root. Starting with 2.0.0 the library has switched to use the standard `Logging` configuration section root.
For backwards compatibility if the `Logging` section does not contain a `LogGroup` then the library will fallback to `AWS.Logging`.

```json
"AWS.Logging": {
"Logging": {
"Region": "us-east-1",
"LogGroup": "AspNetCore.WebSample",
"IncludeLogLevel": true,
"IncludeCategory": true,
"IncludeNewline": true,
"IncludeException": true,
"IncludeEventId": false,
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
Expand All @@ -162,30 +173,19 @@ The configuration is setup in the [appsettings.json](/samples/AspNetCore/WebSamp
}
```

In Startup.cs the configuration is built using the config files and assigned to the Configuration property.
In a typical ASP.NET Core application the `Program.cs` file contains a `CreateWebHostBuilder` method. To include AWS.Logger.AspNetCore
add a call to `ConfigureLogging` and in the `Action<ILoggingBuilder>` passed into ConfigureLogging call `AddAWSProvider`. This will look up the configuration
information from the IConfiguration added to the dependency injection system.

```csharp
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();
}
```

The `Configure` method is used to configure the services added to the dependency injection system. This is where
log providers are configured. For the `AWS.Logger.AspNetCore` the configuration for the provider is loaded from
the Configuration property and the AWS provider is added to the `ILoggerFactory`.

```csharp
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());

...
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddAWSProvider();

// When you need logging below set the minimum level. Otherwise the logging framework will default to Informational for external providers.
logging.SetMinimumLevel(LogLevel.Debug);
})
.UseStartup<Startup>();
```
7 changes: 2 additions & 5 deletions samples/AspNetCore/ConsoleSample/ConsoleSample.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AssemblyName>ConsoleSample</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>ConsoleSample</PackageId>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
Expand All @@ -17,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.0" />
</ItemGroup>

</Project>
3 changes: 0 additions & 3 deletions samples/AspNetCore/WebSample/.bowerrc

This file was deleted.

30 changes: 0 additions & 30 deletions samples/AspNetCore/WebSample/Controllers/HomeController.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@{
ViewData["Title"] = "Home Page";
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}

<h2>Congrats on using the AWS Logging library for ASP.NET Core</h2>
Expand Down
25 changes: 25 additions & 0 deletions samples/AspNetCore/WebSample/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace WebSample.Pages
{
public class IndexModel : PageModel
{
ILogger<IndexModel> Logger { get; set; }

public IndexModel(ILogger<IndexModel> logger)
{
Logger = logger;
}

public void OnGet()
{
Logger.LogInformation("Welcome to the AWS Logger. You are viewing the home page");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@using Microsoft.AspNetCore.Http.Features

@{
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
var showBanner = !consentFeature?.CanTrack ?? false;
var cookieString = consentFeature?.CreateConsentCookie();
}

@if (showBanner)
{
<nav id="cookieConsent" class="navbar navbar-default navbar-fixed-top" role="alert">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#cookieConsent .navbar-collapse">
<span class="sr-only">Toggle cookie consent banner</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></span>
</div>
<div class="collapse navbar-collapse">
<p class="navbar-text">
Use this space to summarize your privacy and cookie use policy.
</p>
<div class="navbar-right">
<a asp-page="/Privacy" class="btn btn-info navbar-btn">Learn More</a>
<button type="button" class="btn btn-default navbar-btn" data-cookie-string="@cookieString">Accept</button>
</div>
</div>
</div>
</nav>
<script>
(function () {
document.querySelector("#cookieConsent button[data-cookie-string]").addEventListener("click", function (el) {
document.cookie = el.target.dataset.cookieString;
document.querySelector("#cookieConsent").classList.add("hidden");
}, false);
})();
</script>
}
51 changes: 51 additions & 0 deletions samples/AspNetCore/WebSample/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - WebSample</title>

<environment include="Development">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css" />
</environment>
<environment exclude="Development">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>
</head>
<body>

<partial name="_CookieConsentPartial" />

<div class="container body-content">
@RenderBody()
<hr />
</div>

<environment include="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
</environment>
<environment exclude="Development">
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.3.1.min.js"
asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
asp-fallback-test="window.jQuery"
crossorigin="anonymous"
integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
crossorigin="anonymous"
integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd">
</script>
<script src="~/js/site.min.js" asp-append-version="true"></script>
</environment>

@RenderSection("Scripts", required: false)
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<environment include="Development">
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
</environment>
<environment exclude="Development">
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.17.0/jquery.validate.min.js"
asp-fallback-src="~/lib/jquery-validation/dist/jquery.validate.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator"
crossorigin="anonymous"
integrity="sha384-rZfj/ogBloos6wzLGpPkkOr/gpkBNLZ6b6yLy4o+ok+t/SAKlL5mvXLr0OXNi1Hp">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.9/jquery.validate.unobtrusive.min.js"
asp-fallback-src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive"
crossorigin="anonymous"
integrity="sha384-ifv0TYDWxBHzvAk2Z0n8R434FL1Rlv/Av18DXE43N/1rvHyOG4izKst0f2iSLdds">
</script>
</environment>
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@using WebSample
@using WebSample
@namespace WebSample.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
23 changes: 15 additions & 8 deletions samples/AspNetCore/WebSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebSample
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

host.Run();
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddAWSProvider();

// When you need logging below set the minimum level. Otherwise the logging framework will default to Informational for external providers.
logging.SetMinimumLevel(LogLevel.Debug);
})
.UseStartup<Startup>();
}
}
Loading