From 366b529550dd433911989bad07fb9bd2546b78bf Mon Sep 17 00:00:00 2001 From: enkodellc Date: Tue, 4 Jun 2019 18:07:52 -0700 Subject: [PATCH] Add Serilog for Log Files --- README.md | 7 ++- .../BlazorBoilerplate.Server.csproj | 8 ++++ .../Controllers/AuthorizeController.cs | 37 +++++++++++---- src/BlazorBoilerplate.Server/Program.cs | 34 ++++++++++++-- .../appsettings.Development.json | 47 +++++++++++++++++++ src/BlazorBoilerplate.Server/appsettings.json | 40 ++++++++++++++++ 6 files changed, 159 insertions(+), 14 deletions(-) create mode 100644 src/BlazorBoilerplate.Server/appsettings.Development.json create mode 100644 src/BlazorBoilerplate.Server/appsettings.json diff --git a/README.md b/README.md index 6ad4f1244..449b0521f 100644 --- a/README.md +++ b/README.md @@ -50,5 +50,10 @@ will be depending on how much community support for it is. This project is licensed under the terms of the [MIT license](LICENSE). ## News -### Blazor Boilerplate 0.1.0 + +### 0.1.1 +- Updated Theme / Responsive +- Added Serilog Log Files + +### 0.1.0 - Initial release diff --git a/src/BlazorBoilerplate.Server/BlazorBoilerplate.Server.csproj b/src/BlazorBoilerplate.Server/BlazorBoilerplate.Server.csproj index 440b9e258..bb4b319ac 100644 --- a/src/BlazorBoilerplate.Server/BlazorBoilerplate.Server.csproj +++ b/src/BlazorBoilerplate.Server/BlazorBoilerplate.Server.csproj @@ -16,6 +16,10 @@ + + + + @@ -23,4 +27,8 @@ + + + + diff --git a/src/BlazorBoilerplate.Server/Controllers/AuthorizeController.cs b/src/BlazorBoilerplate.Server/Controllers/AuthorizeController.cs index adbce7ae2..589e1e1d8 100644 --- a/src/BlazorBoilerplate.Server/Controllers/AuthorizeController.cs +++ b/src/BlazorBoilerplate.Server/Controllers/AuthorizeController.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace BlazorBoilerplate.Server.Controllers { @@ -14,30 +15,44 @@ namespace BlazorBoilerplate.Server.Controllers [ApiController] public class AuthorizeController : ControllerBase { + // Logger instance + ILogger _logger; + private readonly UserManager _userManager; private readonly SignInManager _signInManager; - public AuthorizeController(UserManager userManager, SignInManager signInManager) + public AuthorizeController(UserManager userManager, SignInManager signInManager, ILogger logger) { _userManager = userManager; _signInManager = signInManager; + _logger = logger; } [AllowAnonymous] [HttpPost] public async Task Login(LoginParameters parameters) { - if(!ModelState.IsValid) return BadRequest(ModelState.Values.SelectMany(state => state.Errors) - .Select(error => error.ErrorMessage) - .FirstOrDefault()); + if (!ModelState.IsValid) return BadRequest(ModelState.Values.SelectMany(state => state.Errors) + .Select(error => error.ErrorMessage) + .FirstOrDefault()); var user = await _userManager.FindByNameAsync(parameters.UserName); - if (user == null) return BadRequest("User does not exist"); + if (user == null) + { + _logger.LogInformation("User does not exist: {0}", parameters.UserName); + return BadRequest("User does not exist"); + } + var singInResult = await _signInManager.CheckPasswordSignInAsync(user, parameters.Password, false); - if (!singInResult.Succeeded) return BadRequest("Invalid password"); - await _signInManager.SignInAsync(user, parameters.RememberMe); + if (!singInResult.Succeeded) + { + _logger.LogInformation("Invalid password: {0}, {1}", parameters.UserName, parameters.Password); + return BadRequest("Invalid password"); + } + _logger.LogInformation("Logged In: {0}, {1}", parameters.UserName, parameters.Password); + await _signInManager.SignInAsync(user, parameters.RememberMe); return Ok(BuildUserInfo(user)); } @@ -55,6 +70,8 @@ public async Task Register(RegisterParameters parameters) var result = await _userManager.CreateAsync(user, parameters.Password); if (!result.Succeeded) return BadRequest(result.Errors.FirstOrDefault()?.Description); + _logger.LogInformation("New user registered: {0}", user); + return await Login(new LoginParameters { UserName = parameters.UserName, @@ -100,7 +117,8 @@ public async Task SendPasswordResetEmail(string emailAddress) [HttpPost] public async Task Logout() { - await _signInManager.SignOutAsync(); + _logger.LogInformation("User Logged out"); + await _signInManager.SignOutAsync(); return Ok(); } @@ -111,8 +129,7 @@ public async Task UserInfo() var user = await _userManager.GetUserAsync(HttpContext.User); return BuildUserInfo(user); } - - + private UserInfo BuildUserInfo(ApplicationUser user) { return new UserInfo diff --git a/src/BlazorBoilerplate.Server/Program.cs b/src/BlazorBoilerplate.Server/Program.cs index 7f5bc83a7..4f0c3133c 100644 --- a/src/BlazorBoilerplate.Server/Program.cs +++ b/src/BlazorBoilerplate.Server/Program.cs @@ -1,14 +1,41 @@ -using Microsoft.AspNetCore; +using System; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Events; +using Serilog.Sinks.File; +using Serilog.AspNetCore; +using Serilog.Settings.Configuration; +using Microsoft.Extensions.Configuration.Json; +using Microsoft.Extensions.Hosting; namespace BlazorBoilerplate.Server { public class Program { - public static void Main(string[] args) + public static int Main(string[] args) { - BuildWebHost(args).Run(); + var configuration = new ConfigurationBuilder() + .AddJsonFile("appsettings" + (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development ? ".Development.json" : ".json")) + .Build(); + + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .CreateLogger(); + + try + { + Log.Information("Starting web server host"); + BuildWebHost(args).Run(); + return 0; + } + catch (Exception ex) + { + Log.Fatal(ex, "Host terminated unexpectedly"); + return 1; + } } public static IWebHost BuildWebHost(string[] args) => @@ -17,6 +44,7 @@ public static IWebHost BuildWebHost(string[] args) => .AddCommandLine(args) .Build()) .UseStartup() + .UseSerilog() .Build(); } } diff --git a/src/BlazorBoilerplate.Server/appsettings.Development.json b/src/BlazorBoilerplate.Server/appsettings.Development.json new file mode 100644 index 000000000..5aa53cfb5 --- /dev/null +++ b/src/BlazorBoilerplate.Server/appsettings.Development.json @@ -0,0 +1,47 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Server=localhost;Database=blazorboilerplate;Trusted_Connection=True;MultipleActiveResultSets=true" + }, + + "ApplicationUrl": "http://localhost:53414/", + + "SmtpConfig": { + "Host": "smtp.gmail.com", + "Port": 465, + "UseSSL": true, + "Name": "Blazor Boilerplate", + "Username": "support@blazorboilerplate.com", + "EmailAddress": "support@blazorboilerplate.com", + "Password": "xxxxx", + "ReplyToAddress": "support@blazorboilerplate.com" + }, + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + }, + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ], + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "System": "Warning" + } + }, + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs\\log-.log", + "rollingInterval": "Day", + "retainedFileCountLimit": 5 + } + } + ] + }, + "AllowedHosts": "*" +} diff --git a/src/BlazorBoilerplate.Server/appsettings.json b/src/BlazorBoilerplate.Server/appsettings.json new file mode 100644 index 000000000..61b21f9ea --- /dev/null +++ b/src/BlazorBoilerplate.Server/appsettings.json @@ -0,0 +1,40 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Server=localhost;Database=QuickAppPro;Trusted_Connection=True;MultipleActiveResultSets=true" + }, + + "ApplicationUrl": "http://demo.analyticbroker.com", + + "SmtpConfig": { + "Host": "smtp.gmail.com", + "Port": 465, + "UseSSL": true, + "Name": "Blazor Boilerplate", + "Username": "support@blazorboilerplate.com", + "EmailAddress": "support@blazorboilerplate.com", + "Password": "xxxxx", + "ReplyToAddress": "support@blazorboilerplate.com" + }, + + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": { + "Default": "Warning", + "Override": { + "Microsoft": "Warning", + "System": "Warning" + } + }, + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs\\log-.log", + "rollingInterval": "Day", + "retainedFileCountLimit": 30 + } + } + ] + }, + "AllowedHosts": "*" +}