diff --git a/src/Api/Startup.cs b/src/Api/Startup.cs index fb44ee2d44..ef870d0561 100644 --- a/src/Api/Startup.cs +++ b/src/Api/Startup.cs @@ -17,7 +17,6 @@ using Bit.Core.Repositories; using Bit.Core.Services; using SqlServerRepos = Bit.Core.Repositories.SqlServer; using System.Text; -using Loggr.Extensions.Logging; using System.Linq; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.Net.Http.Headers; @@ -29,6 +28,8 @@ using IdentityServer4.Services; using IdentityModel.AspNetCore.OAuth2Introspection; using IdentityServer4.Stores; using Bit.Core.Utilities; +using Serilog; +using Serilog.Events; namespace Bit.Api { @@ -198,34 +199,40 @@ namespace Bit.Api IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, + IApplicationLifetime appLifetime, GlobalSettings globalSettings) { - loggerFactory.AddConsole(); - loggerFactory.AddDebug(); - - if(!env.IsDevelopment()) + if(env.IsProduction()) { - loggerFactory.AddLoggr( - (category, logLevel, eventId) => + Func serilogFilter = (e) => + { + var context = e.Properties["SourceContext"].ToString(); + if(context == typeof(JwtBearerMiddleware).FullName && e.Level == LogEventLevel.Error) { - // Bad security stamp exception - if(category == typeof(JwtBearerMiddleware).FullName && eventId.Id == 3 && logLevel == LogLevel.Error) - { - return false; - } + return false; + } - // IP blocks - if(category == typeof(IpRateLimitMiddleware).FullName && logLevel >= LogLevel.Information) - { - return true; - } + if(context == typeof(IpRateLimitMiddleware).FullName && e.Level == LogEventLevel.Information) + { + return true; + } - return logLevel >= LogLevel.Error; - }, - globalSettings.Loggr.LogKey, - globalSettings.Loggr.ApiKey); + return e.Level >= LogEventLevel.Error; + }; + + var serilog = new LoggerConfiguration() + .Enrich.FromLogContext() + .Filter.ByIncludingOnly(serilogFilter) + .WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri), globalSettings.DocumentDb.Key, + timeToLive: TimeSpan.FromDays(7)) + .CreateLogger(); + + loggerFactory.AddSerilog(serilog); + appLifetime.ApplicationStopped.Register(Log.CloseAndFlush); } + loggerFactory.AddDebug(); + // Rate limiting app.UseMiddleware(); diff --git a/src/Api/project.json b/src/Api/project.json index 31e4ba3db4..3faf7f01a4 100644 --- a/src/Api/project.json +++ b/src/Api/project.json @@ -19,9 +19,10 @@ "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0", "Microsoft.Extensions.Configuration.Binder": "1.1.0", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0", - "Loggr.Extensions.Logging": "1.0.0", "Microsoft.ApplicationInsights.AspNetCore": "1.0.2", - "AspNetCoreRateLimit": "1.0.5" + "AspNetCoreRateLimit": "1.0.5", + "Serilog.Extensions.Logging": "1.3.1", + "Serilog.Sinks.AzureDocumentDb": "3.5.17" }, "tools": { diff --git a/src/Api/settings.json b/src/Api/settings.json index 3ceb60cdb0..9eea02b22f 100644 --- a/src/Api/settings.json +++ b/src/Api/settings.json @@ -29,6 +29,10 @@ }, "storage": { "connectionString": "SECRET" + }, + "documentDb": { + "uri": "SECRET", + "key": "SECRET" } }, "IpRateLimitOptions": { diff --git a/src/Core/GlobalSettings.cs b/src/Core/GlobalSettings.cs index 5fa0bea6aa..1235ee9e03 100644 --- a/src/Core/GlobalSettings.cs +++ b/src/Core/GlobalSettings.cs @@ -11,6 +11,7 @@ public virtual PushSettings Push { get; set; } = new PushSettings(); public virtual StorageSettings Storage { get; set; } = new StorageSettings(); public virtual IdentityServerSettings IdentityServer { get; set; } = new IdentityServerSettings(); + public virtual DocumentDbSettings DocumentDb { get; set; } = new DocumentDbSettings(); public class SqlServerSettings { @@ -47,5 +48,11 @@ { public string CertificateThumbprint { get; set; } } + + public class DocumentDbSettings + { + public string Uri { get; set; } + public string Key { get; set; } + } } }