mirror of
https://github.com/bitwarden/server.git
synced 2024-11-23 12:25:16 +01:00
add new properties to LogContext
This commit is contained in:
parent
72310701d2
commit
aca274a49b
@ -146,7 +146,7 @@ namespace Bit.Api
|
||||
app.UseSerilog(env, appLifetime, globalSettings);
|
||||
|
||||
// Default Middleware
|
||||
app.UseDefaultMiddleware(env);
|
||||
app.UseDefaultMiddleware(env, globalSettings);
|
||||
|
||||
if(!globalSettings.SelfHosted)
|
||||
{
|
||||
|
@ -30,6 +30,8 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Bit.Core.Utilities;
|
||||
using Serilog.Context;
|
||||
|
||||
namespace Bit.Core.Utilities
|
||||
{
|
||||
@ -407,18 +409,33 @@ namespace Bit.Core.Utilities
|
||||
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
|
||||
app.Use(async (httpContext, next) =>
|
||||
{
|
||||
httpContext.Response.OnStarting((state) =>
|
||||
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.Headers.Append("Server-Version", CoreHelpers.GetVersion());
|
||||
return Task.FromResult(0);
|
||||
}, null);
|
||||
|
||||
await next.Invoke();
|
||||
httpContext.Response.OnStarting((state) =>
|
||||
{
|
||||
httpContext.Response.Headers.Append("Server-Version", CoreHelpers.GetVersion());
|
||||
return Task.FromResult(0);
|
||||
}, null);
|
||||
await next.Invoke();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -91,7 +91,7 @@ namespace Bit.Events
|
||||
}
|
||||
|
||||
// Default Middleware
|
||||
app.UseDefaultMiddleware(env);
|
||||
app.UseDefaultMiddleware(env, globalSettings);
|
||||
|
||||
// Add Cors
|
||||
app.UseCors(policy => policy.SetIsOriginAllowed(h => true)
|
||||
|
@ -80,7 +80,7 @@ namespace Bit.Identity
|
||||
app.UseSerilog(env, appLifetime, globalSettings);
|
||||
|
||||
// Default Middleware
|
||||
app.UseDefaultMiddleware(env);
|
||||
app.UseDefaultMiddleware(env, globalSettings);
|
||||
|
||||
if(!globalSettings.SelfHosted)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user