1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 23:52:50 -05:00

add new properties to LogContext

This commit is contained in:
Kyle Spearrin
2019-09-03 14:44:22 -04:00
parent 72310701d2
commit aca274a49b
4 changed files with 27 additions and 10 deletions

View File

@ -146,7 +146,7 @@ namespace Bit.Api
app.UseSerilog(env, appLifetime, globalSettings); app.UseSerilog(env, appLifetime, globalSettings);
// Default Middleware // Default Middleware
app.UseDefaultMiddleware(env); app.UseDefaultMiddleware(env, globalSettings);
if(!globalSettings.SelfHosted) if(!globalSettings.SelfHosted)
{ {

View File

@ -30,6 +30,8 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using System.Linq; using System.Linq;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using Bit.Core.Utilities;
using Serilog.Context;
namespace Bit.Core.Utilities namespace Bit.Core.Utilities
{ {
@ -407,18 +409,33 @@ namespace Bit.Core.Utilities
return globalSettings; return globalSettings;
} }
public static void UseDefaultMiddleware(this IApplicationBuilder app, IHostingEnvironment env) public static void UseDefaultMiddleware(this IApplicationBuilder app,
IHostingEnvironment env, GlobalSettings globalSettings)
{ {
string GetHeaderValue(HttpContext httpContext, string header)
{
if(httpContext.Request.Headers.ContainsKey(header))
{
return httpContext.Request.Headers[header];
}
return null;
}
// Add version information to response headers // Add version information to response headers
app.Use(async (httpContext, next) => app.Use(async (httpContext, next) =>
{
using(LogContext.PushProperty("IPAddress", httpContext.GetIpAddress(globalSettings)))
using(LogContext.PushProperty("UserAgent", GetHeaderValue(httpContext, "user-agent")))
using(LogContext.PushProperty("DeviceType", GetHeaderValue(httpContext, "device-type")))
using(LogContext.PushProperty("Origin", GetHeaderValue(httpContext, "origin")))
{ {
httpContext.Response.OnStarting((state) => httpContext.Response.OnStarting((state) =>
{ {
httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion()); httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion());
return Task.FromResult(0); return Task.FromResult(0);
}, null); }, null);
await next.Invoke(); await next.Invoke();
}
}); });
} }

View File

@ -91,7 +91,7 @@ namespace Bit.Events
} }
// Default Middleware // Default Middleware
app.UseDefaultMiddleware(env); app.UseDefaultMiddleware(env, globalSettings);
// Add Cors // Add Cors
app.UseCors(policy => policy.SetIsOriginAllowed(h => true) app.UseCors(policy => policy.SetIsOriginAllowed(h => true)

View File

@ -80,7 +80,7 @@ namespace Bit.Identity
app.UseSerilog(env, appLifetime, globalSettings); app.UseSerilog(env, appLifetime, globalSettings);
// Default Middleware // Default Middleware
app.UseDefaultMiddleware(env); app.UseDefaultMiddleware(env, globalSettings);
if(!globalSettings.SelfHosted) if(!globalSettings.SelfHosted)
{ {