Skip to content

Commit

Permalink
Added MS Logger to compare against Serilog.
Browse files Browse the repository at this point in the history
  • Loading branch information
jernejk committed Jun 15, 2021
1 parent 33a4cfb commit 6bdf7fb
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
19 changes: 19 additions & 0 deletions AspNetCoreMsLoggerExample.Web/AspNetCoreMsLoggerExample.Web.csproj
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>
58 changes: 58 additions & 0 deletions AspNetCoreMsLoggerExample.Web/Program.cs
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
});
});
}
}
43 changes: 43 additions & 0 deletions AspNetCoreMsLoggerExample.Web/Startup.cs
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 AspNetCoreMsLoggerExample.Web/appsettings.Development.json
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"
}
}
}
11 changes: 11 additions & 0 deletions AspNetCoreMsLoggerExample.Web/appsettings.json
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": "*"
}
6 changes: 6 additions & 0 deletions AspNetCoreSerilogExample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Root", "Root", "{55156C52-2
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreMsLoggerExample.Web", "AspNetCoreMsLoggerExample.Web\AspNetCoreMsLoggerExample.Web.csproj", "{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -22,6 +24,10 @@ Global
{BE382D81-8A74-4167-A7AD-C7FE1DD89C72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE382D81-8A74-4167-A7AD-C7FE1DD89C72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE382D81-8A74-4167-A7AD-C7FE1DD89C72}.Release|Any CPU.Build.0 = Release|Any CPU
{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37AFFF56-53EC-4DE4-9246-1D210CB32CEC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
19 changes: 19 additions & 0 deletions AspNetCoreSerilogExample.sln.startup.json
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": {}
}
}
}
}

0 comments on commit 6bdf7fb

Please sign in to comment.