gpt4 book ai didi

asp.net-core - 使用 Serilog 和 Asp.Net Core 时将用户添加到日志上下文

转载 作者:行者123 更新时间:2023-12-03 23:51:43 24 4
gpt4 key购买 nike

我正在尝试将 Serilog 与我的 ASP.Net Core 1.0 项目一起使用。我似乎无法将当前登录的用户添加到记录的属性中。

有没有人解决这个问题?

我试过这个:

using System.Threading.Tasks;
using Serilog.Context;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
using xxx.Models;

namespace xxx.Utils
{
public class EnrichSerilogContextMiddleware
{
private readonly RequestDelegate _next;
public EnrichSerilogContextMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext httpContext)
{

var username = httpContext.User.Identity.Name;
if (httpContext.User.Identity.IsAuthenticated)
{
var userFullName = (((ClaimsIdentity)httpContext.User.Identity).FindFirst(Member.FullnameClaimName).Value);
var userName = "anyone@gmail.com";
LoggerEnricher.AddEntryPointContext(userFullName, userName);
}
else
{
LoggerEnricher.AddEntryPointContext();
}


await _next(httpContext);
}
}

public static class LoggerEnricher

{
public static void AddEntryPointContext(string userFullName = null, string username = null)
{
if (!string.IsNullOrWhiteSpace(username) || !string.IsNullOrWhiteSpace(userFullName))
{
LogContext.PushProperty("Username", username);
LogContext.PushProperty("UserFullename", userFullName);
}
else
{
LogContext.PushProperty("Username", "Anonymous");
}

}

public static void EnrichLogger(this IApplicationBuilder app)
{
app.UseMiddleware<EnrichSerilogContextMiddleware>();
}
}
}

我通过添加以下内容在 Startup.cs 中触发此操作:
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddSerilog();
app.EnrichLogger();
...
}

但这总是以“匿名”作为用户名结束。

提前致谢

索伦·罗克达尔

最佳答案

只需几行代码,我就能获得经过身份验证的 Active Directory 用户。我对核心身份验证并不是很有经验,尤其是声明,但也许这会让您继续前进,或者至少可以帮助其他与您遇到类似问题但使用 AD 的人。

关键线路是Enrich.FromLogContext()app.Use(async...

public class Startup
{
public IConfigurationRoot Configuration { get; }

public Startup(IHostingEnvironment env)
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext() // Populates a 'User' property on every log entry
.WriteTo.MSSqlServer(Configuration.GetConnectionString("MyDatabase"), "Logs")
.CreateLogger();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.WithFilter(new FilterLoggerSettings
{
{ "Default", LogLevel.Information },
{ "Microsoft", LogLevel.Warning },
{ "System", LogLevel.Warning }
})
.AddSerilog();

app.Use(async (httpContext, next) =>
{
var userName = httpContext.User.Identity.IsAuthenticated ? httpContext.User.Identity.Name : "unknown";
LogContext.PushProperty("User", !String.IsNullOrWhiteSpace(userName) ? userName : "unknown");
await next.Invoke();
});
}
}

对于通过 IIS/Kestrel 的 AD 身份验证,web.config 需要 forwardWindowsAuthToken设置如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<aspNetCore ... forwardWindowsAuthToken="true" />
</system.webServer>
</configuration>

关于asp.net-core - 使用 Serilog 和 Asp.Net Core 时将用户添加到日志上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39896465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com