Skip to content

Commit 6bdf7fb

Browse files
committed
Added MS Logger to compare against Serilog.
1 parent 33a4cfb commit 6bdf7fb

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup Condition="'$(Configuration)'=='DEBUG'">
8+
<PackageReference Include="Seq.Extensions.Logging" Version="6.0.0" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<Compile Include="..\AspNetCoreSerilogExample.Web\Controllers\TestController.cs" Link="Controllers\TestController.cs" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<Folder Include="Controllers\" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
11+
namespace AspNetCoreMsLoggerExample.Web
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
try
18+
{
19+
using IHost host = CreateHostBuilder(args).Build();
20+
host.Run();
21+
}
22+
catch (Exception ex)
23+
{
24+
// Loading configuration or running the application failed.
25+
// This will create a logger that can be captured by Azure logger.
26+
// To enable Azure logger, in Azure Portal:
27+
// 1. Go to WebApp
28+
// 2. App Service logs
29+
// 3. Enable "Application Logging (Filesystem)", "Application Logging (Filesystem)" and "Detailed error messages"
30+
// 4. Set Retention Period (Days) to 10 or similar value
31+
// 5. Save settings
32+
// 6. Under Overview, restart web app
33+
// 7. Go to Log Stream and observe the logs
34+
Console.WriteLine("Host terminated unexpectedly: " + ex);
35+
}
36+
}
37+
38+
public static IHostBuilder CreateHostBuilder(string[] args)
39+
=> Host.CreateDefaultBuilder(args)
40+
.ConfigureWebHostDefaults(webBuilder =>
41+
{
42+
webBuilder.UseStartup<Startup>()
43+
.CaptureStartupErrors(true)
44+
.ConfigureAppConfiguration(config =>
45+
{
46+
config
47+
// Used for local settings like connection strings.
48+
.AddJsonFile("appsettings.Local.json", optional: true);
49+
})
50+
.ConfigureLogging((hostingContext, loggingBuilder) => {
51+
#if DEBUG
52+
// Add Seq for local dev.
53+
loggingBuilder.AddSeq();
54+
#endif
55+
});
56+
});
57+
}
58+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
8+
namespace AspNetCoreMsLoggerExample.Web
9+
{
10+
public class Startup
11+
{
12+
public Startup(IConfiguration configuration)
13+
{
14+
Configuration = configuration;
15+
}
16+
17+
public IConfiguration Configuration { get; }
18+
19+
// This method gets called by the runtime. Use this method to add services to the container.
20+
public void ConfigureServices(IServiceCollection services)
21+
{
22+
services.AddControllers();
23+
services.AddLogging();
24+
}
25+
26+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
27+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
28+
{
29+
if (env.IsDevelopment())
30+
{
31+
app.UseDeveloperExceptionPage();
32+
}
33+
34+
app.UseRouting();
35+
36+
app.UseEndpoints(endpoints =>
37+
{
38+
endpoints.MapControllerRoute(name: "default", pattern: "{controller}/{action=Index}/{id?}");
39+
endpoints.MapGet("", context => context.Response.WriteAsync("Hello World!"));
40+
});
41+
}
42+
}
43+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"DetailedErrors": true,
3+
"Logging": {
4+
"LogLevel": {
5+
"Default": "Information",
6+
"Microsoft": "Warning",
7+
"Microsoft.Hosting.Lifetime": "Information"
8+
}
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"System": "Information",
6+
"Microsoft": "Information",
7+
"Microsoft.EntityFrameworkCore": "Information"
8+
}
9+
},
10+
"AllowedHosts": "*"
11+
}

AspNetCoreSerilogExample.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Root", "Root", "{55156C52-2
1212
README.md = README.md
1313
EndProjectSection
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreMsLoggerExample.Web", "AspNetCoreMsLoggerExample.Web\AspNetCoreMsLoggerExample.Web.csproj", "{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -22,6 +24,10 @@ Global
2224
{BE382D81-8A74-4167-A7AD-C7FE1DD89C72}.Debug|Any CPU.Build.0 = Debug|Any CPU
2325
{BE382D81-8A74-4167-A7AD-C7FE1DD89C72}.Release|Any CPU.ActiveCfg = Release|Any CPU
2426
{BE382D81-8A74-4167-A7AD-C7FE1DD89C72}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}.Release|Any CPU.Build.0 = Release|Any CPU
2531
EndGlobalSection
2632
GlobalSection(SolutionProperties) = preSolution
2733
HideSolutionNode = FALSE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
This is a configuration file for the SwitchStartupProject Visual Studio Extension
3+
See https://heptapod.host/thirteen/switchstartupproject/blob/branch/current/Configuration.md
4+
*/
5+
{
6+
/* Configuration File Version */
7+
"Version": 3,
8+
9+
/* Create an item in the dropdown list for each project in the solution? */
10+
"ListAllProjects": true,
11+
"MultiProjectConfigurations": {
12+
"Both": {
13+
"Projects": {
14+
"AspNetCoreMsLoggerExample.Web": {},
15+
"AspNetCoreSerilogExample.Web": {}
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)