1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-07 05:58:13 -05:00

replace loggr with serilog

This commit is contained in:
Kyle Spearrin 2017-01-14 23:24:02 -05:00
parent 31c3835dd3
commit 49f7857d2e
4 changed files with 42 additions and 23 deletions

View File

@ -17,7 +17,6 @@ using Bit.Core.Repositories;
using Bit.Core.Services; using Bit.Core.Services;
using SqlServerRepos = Bit.Core.Repositories.SqlServer; using SqlServerRepos = Bit.Core.Repositories.SqlServer;
using System.Text; using System.Text;
using Loggr.Extensions.Logging;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
@ -29,6 +28,8 @@ using IdentityServer4.Services;
using IdentityModel.AspNetCore.OAuth2Introspection; using IdentityModel.AspNetCore.OAuth2Introspection;
using IdentityServer4.Stores; using IdentityServer4.Stores;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Serilog;
using Serilog.Events;
namespace Bit.Api namespace Bit.Api
{ {
@ -198,34 +199,40 @@ namespace Bit.Api
IApplicationBuilder app, IApplicationBuilder app,
IHostingEnvironment env, IHostingEnvironment env,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IApplicationLifetime appLifetime,
GlobalSettings globalSettings) GlobalSettings globalSettings)
{ {
loggerFactory.AddConsole(); if(env.IsProduction())
loggerFactory.AddDebug();
if(!env.IsDevelopment())
{ {
loggerFactory.AddLoggr( Func<LogEvent, bool> serilogFilter = (e) =>
(category, logLevel, eventId) => {
var context = e.Properties["SourceContext"].ToString();
if(context == typeof(JwtBearerMiddleware).FullName && e.Level == LogEventLevel.Error)
{ {
// Bad security stamp exception return false;
if(category == typeof(JwtBearerMiddleware).FullName && eventId.Id == 3 && logLevel == LogLevel.Error) }
{
return false;
}
// IP blocks if(context == typeof(IpRateLimitMiddleware).FullName && e.Level == LogEventLevel.Information)
if(category == typeof(IpRateLimitMiddleware).FullName && logLevel >= LogLevel.Information) {
{ return true;
return true; }
}
return logLevel >= LogLevel.Error; return e.Level >= LogEventLevel.Error;
}, };
globalSettings.Loggr.LogKey,
globalSettings.Loggr.ApiKey); 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 // Rate limiting
app.UseMiddleware<CustomIpRateLimitMiddleware>(); app.UseMiddleware<CustomIpRateLimitMiddleware>();

View File

@ -19,9 +19,10 @@
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
"Microsoft.Extensions.Configuration.Binder": "1.1.0", "Microsoft.Extensions.Configuration.Binder": "1.1.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
"Loggr.Extensions.Logging": "1.0.0",
"Microsoft.ApplicationInsights.AspNetCore": "1.0.2", "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": { "tools": {

View File

@ -29,6 +29,10 @@
}, },
"storage": { "storage": {
"connectionString": "SECRET" "connectionString": "SECRET"
},
"documentDb": {
"uri": "SECRET",
"key": "SECRET"
} }
}, },
"IpRateLimitOptions": { "IpRateLimitOptions": {

View File

@ -11,6 +11,7 @@
public virtual PushSettings Push { get; set; } = new PushSettings(); public virtual PushSettings Push { get; set; } = new PushSettings();
public virtual StorageSettings Storage { get; set; } = new StorageSettings(); public virtual StorageSettings Storage { get; set; } = new StorageSettings();
public virtual IdentityServerSettings IdentityServer { get; set; } = new IdentityServerSettings(); public virtual IdentityServerSettings IdentityServer { get; set; } = new IdentityServerSettings();
public virtual DocumentDbSettings DocumentDb { get; set; } = new DocumentDbSettings();
public class SqlServerSettings public class SqlServerSettings
{ {
@ -47,5 +48,11 @@
{ {
public string CertificateThumbprint { get; set; } public string CertificateThumbprint { get; set; }
} }
public class DocumentDbSettings
{
public string Uri { get; set; }
public string Key { get; set; }
}
} }
} }