-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MS Logger to compare against Serilog.
- Loading branch information
Showing
7 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
AspNetCoreMsLoggerExample.Web/AspNetCoreMsLoggerExample.Web.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition="'$(Configuration)'=='DEBUG'"> | ||
<PackageReference Include="Seq.Extensions.Logging" Version="6.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\AspNetCoreSerilogExample.Web\Controllers\TestController.cs" Link="Controllers\TestController.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Controllers\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace AspNetCoreMsLoggerExample.Web | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
try | ||
{ | ||
using IHost host = CreateHostBuilder(args).Build(); | ||
host.Run(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Loading configuration or running the application failed. | ||
// This will create a logger that can be captured by Azure logger. | ||
// To enable Azure logger, in Azure Portal: | ||
// 1. Go to WebApp | ||
// 2. App Service logs | ||
// 3. Enable "Application Logging (Filesystem)", "Application Logging (Filesystem)" and "Detailed error messages" | ||
// 4. Set Retention Period (Days) to 10 or similar value | ||
// 5. Save settings | ||
// 6. Under Overview, restart web app | ||
// 7. Go to Log Stream and observe the logs | ||
Console.WriteLine("Host terminated unexpectedly: " + ex); | ||
} | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) | ||
=> Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder.UseStartup<Startup>() | ||
.CaptureStartupErrors(true) | ||
.ConfigureAppConfiguration(config => | ||
{ | ||
config | ||
// Used for local settings like connection strings. | ||
.AddJsonFile("appsettings.Local.json", optional: true); | ||
}) | ||
.ConfigureLogging((hostingContext, loggingBuilder) => { | ||
#if DEBUG | ||
// Add Seq for local dev. | ||
loggingBuilder.AddSeq(); | ||
#endif | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace AspNetCoreMsLoggerExample.Web | ||
{ | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
|
||
// This method gets called by the runtime. Use this method to add services to the container. | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddControllers(); | ||
services.AddLogging(); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseRouting(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllerRoute(name: "default", pattern: "{controller}/{action=Index}/{id?}"); | ||
endpoints.MapGet("", context => context.Response.WriteAsync("Hello World!")); | ||
}); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
AspNetCoreMsLoggerExample.Web/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"DetailedErrors": true, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"System": "Information", | ||
"Microsoft": "Information", | ||
"Microsoft.EntityFrameworkCore": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
This is a configuration file for the SwitchStartupProject Visual Studio Extension | ||
See https://heptapod.host/thirteen/switchstartupproject/blob/branch/current/Configuration.md | ||
*/ | ||
{ | ||
/* Configuration File Version */ | ||
"Version": 3, | ||
|
||
/* Create an item in the dropdown list for each project in the solution? */ | ||
"ListAllProjects": true, | ||
"MultiProjectConfigurations": { | ||
"Both": { | ||
"Projects": { | ||
"AspNetCoreMsLoggerExample.Web": {}, | ||
"AspNetCoreSerilogExample.Web": {} | ||
} | ||
} | ||
} | ||
} |