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

Changes to add Serilog #2

Draft
wants to merge 9 commits into
base: no-serilog-integration
from
@@ -0,0 +1,11 @@
language: csharp
mono: none
sudo: required
dist: xenial
dotnet: 2.2
script:
- dotnet restore
- dotnet build AspNetCoreSerilogExample.sln -c Release
global:
- DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true
- DOTNET_CLI_TELEMETRY_OPTOUT=1
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
@@ -9,5 +9,31 @@
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<!-- Serilog dependencies -->
<PackageReference Include="Serilog" Version="2.8.0" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
<PackageReference Include="Serilog.Exceptions" Version="5.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.4" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.3.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
<!-- Make sure all of the necessary appsettings are included with the application. -->
<Content Update="appsettings*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</Content>
<Content Update="appsettings.Local.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>

</Project>
@@ -1,17 +1,73 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;
using System.Diagnostics;

namespace AspNetCoreSerilogExample.Web
{
/// <summary>
/// Logging based on blog post https://jkdev.me/asp-net-core-serilog/
/// </summary>
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
// There are 2 reasons why we are building logging this early and is used only for logging.
// 1. We want to log any issues that might happen while the server is spinning up.
// Those are hard to find bugs and quite often are not logged properly.
// 2. Unfortunately ".UseConfiguration()" doesn't work correctly in the .NET Core 2.2.5 (the version I made this demo).
// It will ignore the configuration and will load default configuration.
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddJsonFile("appsettings.local.json", optional: true)
.Build();

// Lets make sure that if creating web host fails, we can log that error.
var loggerConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
.Enrich.WithProperty("ApplicationName", typeof(Program).Assembly.GetName().Name);

#if DEBUG
// Used to filter out potentially bad data due debugging.
// Very useful when doing Seq dashboards and want to remove logs under debugging session.
loggerConfiguration.Enrich.WithProperty("DebuggerAttached", Debugger.IsAttached);
#endif

// When using ".UseSerilog()" it will use "Log.Logger".
Log.Logger = loggerConfiguration.CreateLogger();

try
{
// In some rare cases, creating web host can fail.
// This style of logging increases the chances of logging this issue.
Log.Logger.Information("Bootstrapping web app...");
using (var host = CreateWebHostBuilder(args).Build())
{
host.Run();
}
}
catch (Exception e)
{
// Happens rarely but when it does, you'll thank me. :)
Log.Logger.Fatal(e, "Unable to bootstrap web app.");
}

// Make sure all the log sinks have processed the last log before closing the application.
Log.CloseAndFlush();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
=> WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration(configuration =>
{
// It's a good practice to add local settings for local dev.
configuration.AddJsonFile("appsettings.local.json", optional: true);
})
.UseSerilog();
}
}
@@ -1,4 +1,42 @@
{
"Serilog": {
"Using": [ "Serilog.Exceptions", "Serilog", "Serilog.Sinks.Console", "Serilog.Sinks.Seq" ],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"System": "Information",
"Microsoft": "Information",
"Microsoft.EntityFrameworkCore": "Information"
}
},
"WriteTo": [
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341",
"apiKey": "none",
"restrictedToMinimumLevel": "Verbose"
}
},
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"restrictedToMinimumLevel": "Information"
}
}
]
}
}
],
"Enrich": [ "FromLogContext", "WithExceptionDetails" ],
"Properties": {
"Environment": "LocalDev"
}
},
"Logging": {
"LogLevel": {
"Default": "Warning"
@@ -3,7 +3,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28922.388
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreSerilogExample.Web", "AspNetCoreSerilogExample.Web\AspNetCoreSerilogExample.Web.csproj", "{BE382D81-8A74-4167-A7AD-C7FE1DD89C72}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCoreSerilogExample.Web", "AspNetCoreSerilogExample.Web\AspNetCoreSerilogExample.Web.csproj", "{BE382D81-8A74-4167-A7AD-C7FE1DD89C72}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Root", "Root", "{55156C52-24F7-4C9D-935A-4BCD2AEA6CF0}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
.travis.yml = .travis.yml
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -1,2 +1,6 @@
[![Build Status](https://travis-ci.com/jernejk/AspNetCoreSerilogExample.svg?branch=master)](https://travis-ci.com/jernejk/AspNetCoreSerilogExample)

# AspNetCoreSerilogExample
This is an example of how to create a ASP .NET Core app with Serilog (.NET Core 2.2+)

Check my blog post for more details: [ASP.NET Core + Serilog + Seq](https://jkdev.me/asp-net-core-serilog/)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.