Skip to content

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed Apr 23, 2021
1 parent 8d70b17 commit 7f703b9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
38 changes: 38 additions & 0 deletions src/DevNews.WebApp/Infrastructure/Logging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Exceptions;
using Serilog.Sinks.SystemConsole.Themes;

namespace DevNews.WebApp.Infrastructure
{
public class SerilogOptions
{
public bool ConsoleEnabled { get; set; } = true;
public string MinimumLevel { get; set; } = "Information";
public string Format { get; set; } = "compact";
}

public static class SerilogExtensions
{
public static IHostBuilder UseLogger(this IHostBuilder hostBuilder, string applicationName = null)
{
return hostBuilder.ConfigureLogging(builder =>
{
var conf = new LoggerConfiguration()
.MinimumLevel.Is(LogEventLevel.Information)
.Enrich.FromLogContext()
.Enrich.WithProperty("ApplicationName", applicationName)
.Enrich.WithEnvironmentUserName()
.Enrich.WithProcessId()
.Enrich.WithProcessName()
.Enrich.WithThreadId()
.Enrich.WithExceptionDetails();

conf.WriteTo.Console(theme: AnsiConsoleTheme.Code);
conf.WriteTo.Trace();
builder.AddSerilog(conf.CreateLogger());
});
}
}
}
3 changes: 1 addition & 2 deletions src/DevNews.WebApp/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ else

private long numberOfPages = 0;

private const int pageSize = 20;
private const int pageSize = 20;

protected override async Task OnParametersSetAsync()
{
Expand All @@ -150,7 +150,6 @@ else
private async Task LoadArticles()
{
var page = Page ?? 1;
_logger.LogInformation("Load Articles by Param Page={Page} and PageSize={PageSize}", page, pageSize);
articles = await _getArticles.Execute(new GetArticlesQuery(page, pageSize));
}

Expand Down
8 changes: 2 additions & 6 deletions src/DevNews.WebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DevNews.WebApp.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace DevNews.WebApp
{
Expand All @@ -18,6 +13,7 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseLogger("DevNews.WebApp")
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
}
13 changes: 5 additions & 8 deletions src/DevNews.WebApp/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"Articles": "mongodb://root:rootpassword@127.0.0.1:27017"
},
"AllowedHosts": "*"
"Serilog": {
"MinimumLevel": "Information",
"ConsoleEnabled": true,
"Format": "colored"
}
}
9 changes: 9 additions & 0 deletions src/DevNews.WebApp/paket.references
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Serilog.Enrichers.Environment
Serilog.Enrichers.Process
Serilog.Enrichers.Thread
Serilog.Exceptions
Serilog.Sinks.Async
Serilog.Sinks.Trace
Serilog.Sinks.Seq
Serilog.Formatting.Elasticsearch
Serilog.AspNetCore

0 comments on commit 7f703b9

Please sign in to comment.