diff --git a/src/DevNews.WebApp/Infrastructure/Logging.cs b/src/DevNews.WebApp/Infrastructure/Logging.cs new file mode 100644 index 0000000..a2556b6 --- /dev/null +++ b/src/DevNews.WebApp/Infrastructure/Logging.cs @@ -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()); + }); + } + } +} \ No newline at end of file diff --git a/src/DevNews.WebApp/Pages/Index.razor b/src/DevNews.WebApp/Pages/Index.razor index 8e4e6ee..5750413 100644 --- a/src/DevNews.WebApp/Pages/Index.razor +++ b/src/DevNews.WebApp/Pages/Index.razor @@ -123,7 +123,7 @@ else private long numberOfPages = 0; - private const int pageSize = 20; + private const int pageSize = 20; protected override async Task OnParametersSetAsync() { @@ -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)); } diff --git a/src/DevNews.WebApp/Program.cs b/src/DevNews.WebApp/Program.cs index b7aa66e..885e67f 100644 --- a/src/DevNews.WebApp/Program.cs +++ b/src/DevNews.WebApp/Program.cs @@ -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 { @@ -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(); }); } } \ No newline at end of file diff --git a/src/DevNews.WebApp/appsettings.json b/src/DevNews.WebApp/appsettings.json index a8cbc64..5870176 100644 --- a/src/DevNews.WebApp/appsettings.json +++ b/src/DevNews.WebApp/appsettings.json @@ -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" + } } diff --git a/src/DevNews.WebApp/paket.references b/src/DevNews.WebApp/paket.references index e69de29..a980d98 100644 --- a/src/DevNews.WebApp/paket.references +++ b/src/DevNews.WebApp/paket.references @@ -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 \ No newline at end of file