From ef3853d5070c09a4b33ec7f74d82755a050a13b0 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 6 Feb 2016 23:45:33 -0500 Subject: [PATCH] Added loggr logging for production environment. --- src/Api/Startup.cs | 15 ++++++++++++++- .../Utilities/ExceptionHandlerFilterAttribute.cs | 4 ++++ src/Api/project.json | 3 ++- src/Api/settings.json | 4 ++++ src/Core/GlobalSettings.cs | 9 ++++++++- src/Core/Services/MailService.cs | 2 +- 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Api/Startup.cs b/src/Api/Startup.cs index 00dc03646..77bfd4a1e 100644 --- a/src/Api/Startup.cs +++ b/src/Api/Startup.cs @@ -16,6 +16,7 @@ using Bit.Core.Identity; using Bit.Core.Repositories; using Bit.Core.Services; using Repos = Bit.Core.Repositories.SqlServer; +using Loggr.Extensions.Logging; namespace Bit.Api { @@ -131,12 +132,24 @@ namespace Bit.Api }); } - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) + public void Configure( + IApplicationBuilder app, + IHostingEnvironment env, + ILoggerFactory loggerFactory, + GlobalSettings globalSettings) { loggerFactory.MinimumLevel = LogLevel.Information; loggerFactory.AddConsole(); loggerFactory.AddDebug(); + if(!env.IsDevelopment()) + { + loggerFactory.AddLoggr( + LogLevel.Error, + globalSettings.Loggr.LogKey, + globalSettings.Loggr.ApiKey); + } + // Add the platform handler to the request pipeline. app.UseIISPlatformHandler(); diff --git a/src/Api/Utilities/ExceptionHandlerFilterAttribute.cs b/src/Api/Utilities/ExceptionHandlerFilterAttribute.cs index 9a282fc30..587041c4c 100644 --- a/src/Api/Utilities/ExceptionHandlerFilterAttribute.cs +++ b/src/Api/Utilities/ExceptionHandlerFilterAttribute.cs @@ -6,6 +6,7 @@ using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.Filters; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace Bit.Api.Utilities { @@ -47,6 +48,9 @@ namespace Bit.Api.Utilities } else { + var logger = context.HttpContext.ApplicationServices.GetRequiredService>(); + logger.LogError(exception.Message, exception); + errorModel.Message = "An unhandled server error has occured."; context.HttpContext.Response.StatusCode = 500; } diff --git a/src/Api/project.json b/src/Api/project.json index bcd018e99..03540b45c 100644 --- a/src/Api/project.json +++ b/src/Api/project.json @@ -21,7 +21,8 @@ "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final", "Microsoft.AspNet.Cors": "6.0.0-rc1-final", "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final", - "NewRelic.Azure.WebSites": "5.13.30" + "NewRelic.Azure.WebSites": "5.13.30", + "Loggr.Extensions.Logging": "1.0.1-rc1-final" }, "commands": { diff --git a/src/Api/settings.json b/src/Api/settings.json index b2bcda01f..de8a1cef9 100644 --- a/src/Api/settings.json +++ b/src/Api/settings.json @@ -15,6 +15,10 @@ "mail": { "apiKey": "SECRET", "replyToEmail": "do-not-reply@bitwarden.com" + }, + "loggr": { + "logKey": "SECRET", + "apiKey": "SECRET" } } } diff --git a/src/Core/GlobalSettings.cs b/src/Core/GlobalSettings.cs index 12e093d7a..bd1718a39 100644 --- a/src/Core/GlobalSettings.cs +++ b/src/Core/GlobalSettings.cs @@ -10,6 +10,7 @@ namespace Bit.Core public virtual DocumentDBSettings DocumentDB { get; set; } = new DocumentDBSettings(); public virtual SqlServerSettings SqlServer { get; set; } = new SqlServerSettings(); public virtual MailSettings Mail { get; set; } = new MailSettings(); + public virtual LoggrSettings Loggr { get; set; } = new LoggrSettings(); public class DocumentDBSettings { @@ -27,8 +28,14 @@ namespace Bit.Core public class MailSettings { - public string APIKey { get; set; } + public string ApiKey { get; set; } public string ReplyToEmail { get; set; } } + + public class LoggrSettings + { + public string LogKey { get; set; } + public string ApiKey { get; set; } + } } } diff --git a/src/Core/Services/MailService.cs b/src/Core/Services/MailService.cs index b741573b3..6bf686c20 100644 --- a/src/Core/Services/MailService.cs +++ b/src/Core/Services/MailService.cs @@ -26,7 +26,7 @@ namespace Bit.Core.Services public MailService(GlobalSettings globalSettings) { _globalSettings = globalSettings; - _web = new Web(_globalSettings.Mail.APIKey); + _web = new Web(_globalSettings.Mail.ApiKey); } public async Task SendAlreadyRegisteredEmailAsync(string registrantEmailAddress)