Skip to content

Commit

Permalink
Adding Serilog, AppInsights Sink, and updated existing log messages t… (
Browse files Browse the repository at this point in the history
ardalis#325)

* Adding Serilog, AppInsights Sink, and updated existing log messages to use structured logging

* Setting up logging to a local text file, updating gitignore so they aren't checked in

* Adding sqlite-shm and wal to gitignore

Co-authored-by: Steve Smith <steve@kentsmiths.com>
  • Loading branch information
efleming18 and ardalis authored Feb 24, 2022
1 parent 793c046 commit fa0776e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/Clean.Architecture.Infrastructure/EmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/Clean.Architecture.Web/Clean.Architecture.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" PrivateAssets="all" Version="6.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" PrivateAssets="All" Version="6.0.1" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
Expand Down
9 changes: 4 additions & 5 deletions src/Clean.Architecture.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -97,7 +96,7 @@
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
logger.LogError(ex, "An error occurred seeding the DB. {exceptionMessage}", ex.Message);
}
}

Expand Down
32 changes: 25 additions & 7 deletions src/Clean.Architecture.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
// }
//}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit fa0776e

Please sign in to comment.