diff --git a/.gitignore b/.gitignore index 8f1a18c85..96baec1d9 100644 --- a/.gitignore +++ b/.gitignore @@ -255,9 +255,14 @@ paket-files/ .idea/ *.sln.iml *.sqlite +*.sqlite-shm +*.sqlite-wal # Nuget files DeploymentSettings.props Directory.Build.props Directory.Build.targets Packages.props + +#Local Log Files +log*.txt diff --git a/src/Clean.Architecture.Infrastructure/EmailSender.cs b/src/Clean.Architecture.Infrastructure/EmailSender.cs index 8bdc6e06f..73bd30cb8 100644 --- a/src/Clean.Architecture.Infrastructure/EmailSender.cs +++ b/src/Clean.Architecture.Infrastructure/EmailSender.cs @@ -27,6 +27,6 @@ public async Task SendEmailAsync(string to, string from, string subject, string }; message.To.Add(new MailAddress(to)); await emailClient.SendMailAsync(message); - _logger.LogWarning($"Sending email to {to} from {from} with subject {subject}."); + _logger.LogWarning("Sending email to {to} from {from} with subject {subject}.", to, from, subject); } } diff --git a/src/Clean.Architecture.Web/Clean.Architecture.Web.csproj b/src/Clean.Architecture.Web/Clean.Architecture.Web.csproj index 5e3b56cad..0257d6d77 100644 --- a/src/Clean.Architecture.Web/Clean.Architecture.Web.csproj +++ b/src/Clean.Architecture.Web/Clean.Architecture.Web.csproj @@ -19,6 +19,7 @@ + diff --git a/src/Clean.Architecture.Web/Program.cs b/src/Clean.Architecture.Web/Program.cs index 0d679d936..6f6269647 100644 --- a/src/Clean.Architecture.Web/Program.cs +++ b/src/Clean.Architecture.Web/Program.cs @@ -6,12 +6,14 @@ using Clean.Architecture.Infrastructure.Data; using Clean.Architecture.Web; using Microsoft.OpenApi.Models; - +using Serilog; var builder = WebApplication.CreateBuilder(args); builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); +builder.Host.UseSerilog((_, config) => config.ReadFrom.Configuration(builder.Configuration)); + builder.Services.Configure(options => { options.CheckConsentNeeded = context => true; @@ -47,9 +49,6 @@ containerBuilder.RegisterModule(new DefaultInfrastructureModule(builder.Environment.EnvironmentName == "Development")); }); - -builder.Logging.ClearProviders(); -builder.Logging.AddConsole(); //builder.Logging.AddAzureWebAppDiagnostics(); add this if deploying to Azure var app = builder.Build(); @@ -97,7 +96,7 @@ catch (Exception ex) { var logger = services.GetRequiredService>(); - logger.LogError(ex, "An error occurred seeding the DB."); + logger.LogError(ex, "An error occurred seeding the DB. {exceptionMessage}", ex.Message); } } diff --git a/src/Clean.Architecture.Web/appsettings.json b/src/Clean.Architecture.Web/appsettings.json index 0e4c58818..7331e3538 100644 --- a/src/Clean.Architecture.Web/appsettings.json +++ b/src/Clean.Architecture.Web/appsettings.json @@ -3,12 +3,30 @@ "DefaultConnection": "Server=(localdb)\\v11.0;Database=cleanarchitecture;Trusted_Connection=True;MultipleActiveResultSets=true", "SqliteConnection": "Data Source=database.sqlite" }, - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } + "Serilog": { + "MinimumLevel": { + "Default": "Information" + }, + "WriteTo": [ + { + "Name": "Console" + }, + { + "Name": "File", + "Args": { + "path": "log.txt", + "rollingInterval": "Day" + } + } + //Uncomment this section if you'd like to push your logs to Azure Application Insights + //Full list of Serilog Sinks can be found here: https://github.com/serilog/serilog/wiki/Provided-Sinks + //{ + // "Name": "ApplicationInsights", + // "Args": { + // "instrumentationKey": "", //Fill in with your ApplicationInsights InstrumentationKey + // "telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights" + // } + //} + ] } } diff --git a/tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs b/tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs index b44b30bb6..efc808f41 100644 --- a/tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs +++ b/tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs @@ -53,7 +53,7 @@ protected override IHost CreateHost(IHostBuilder builder) catch (Exception ex) { logger.LogError(ex, "An error occurred seeding the " + - $"database with test messages. Error: {ex.Message}"); + "database with test messages. Error: {exceptionMessage}", ex.Message); } }