Код: Выделить всё
public class Program
{
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.Build();
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.WriteTo.MSSqlServer(Configuration.GetConnectionString("DefaultConnection"), "dbo.Log")
.Enrich.WithThreadId()
.Enrich.WithProperty("Version", "1.0.0")
.CreateLogger();
try
{
BuildWebHost(args).Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.UseSerilog()
.Build();
}
< /code>
Теперь я хочу добавить httpcontext.current.user.identity.name < /code> во все сообщения журнала. < /p>
Я попытался создать новый класс обогащения после документации https://github.com/serilog/serilog/wiki/configuration-basics#enrichers
class UsernameEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory, HttpContext httpContext)
{
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
"Username", httpContext.User.Identity.Name));
}
}
< Br /> Я также попытался установить пакет Nuget serilog.web.classic < /strong>, который содержит обогатитель имени пользователя, но существует конфликт между целевой структурой .NET Framework и .NET Core, поэтому я не могу использовать этот плагин . < /p>
есть идея? < /p>
Подробнее здесь: https://stackoverflow.com/questions/512 ... to-serilog
Мобильная версия