gpt4 book ai didi

c# - 如何使用 NLog 在文件目标中使用 session 变量?

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:22 26 4
gpt4 key购买 nike

我想将每个 session 放在不同的文件夹中:${basedir}/logs/session/2016-08-11.log

但我没有找到如何在 NLog.config 中使用 session 变量,例如

<targets>
<target xsi:type="File" name="Example" fileName="${basedir}/logs/${aspnet-session:Variable=Login}/${longdate}.log" layout="${message}" />
</targets>

不知道有没有可能?有什么想法吗?

最佳答案

对于 ASP.NET 4,您应该包括 NLog.Web NuGet 包

对于 ASP.NET Core,您需要 NLog.Web.AspNetCore和以下配置(非核心不需要)

在你的 nlog.config 中:

  <extensions>
<!--enable NLog.Web for ASP.NET Core-->
<add assembly="NLog.Web.AspNetCore"/>
</extensions>

在你的 startup.cs 中

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//add NLog to ASP.NET Core
loggerFactory.AddNLog();

//add NLog.Web
app.AddNLogWeb();

在 project.json 中:

"dependencies": {
"NLog.Extensions.Logging": "1.0.0-rtm-alpha4",
"NLog.Web.AspNetCore": "4.2.3"
},

关于c# - 如何使用 NLog 在文件目标中使用 session 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38897156/

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