小编Sør*_*dal的帖子

使用Serilog和Asp.Net Core时,将用户添加到日志上下文中

我正在尝试将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, …
Run Code Online (Sandbox Code Playgroud)

serilog asp.net-core

13
推荐指数
3
解决办法
5464
查看次数

如何将输入元素集中在Asp.Net Core(MVC6)视图上

您是否知道在Asp.Net Core的剃刀视图上为焦点添加焦点的方法.我知道这可以通过javascript完成,但是还有一个标签吗?在input元素或包含表单元素上.

提前致谢

SørenRokkedal

forms asp.net-core-mvc tag-helpers

2
推荐指数
1
解决办法
1925
查看次数