NLog.Web: aspnet-sessionId not working for Asp.Net Core (.Net 4.7)
We are trying to use aspnet-sessionid in an ASP.Net core application (.net version 4.7) and nLog is not showing it in the logs at all.
We tried with multiple different configuration values, but none produce any output.
Is this supposed to be working in ASP.Net Core and .Net 4.7?
For reference:
In Startup.cs
public IServiceProvider ConfigureIoC(IServiceCollection services)
{
var container = new Container(new RuntimeRegistry());
container.Configure(config => config.Populate(services));
services.AddSingleton<Microsoft.AspNetCore.Http.IHttpContextAccessor, HttpContextAccessor>();
return container.GetInstance<IServiceProvider>();
}
// 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)
{
//loggerFactory.AddConsole(Configuration.GetSection("Logging"));
//loggerFactory.AddDebug();
loggerFactory.AddNLog();
env.ConfigureNLog("nlog.config");
app.UseMvc();
}
nLog.config file
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLogEntry.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogEntryLevel="Warn"
internalLogEntryFile="c:\temp\internal.txt">
<extensions>
<add assembly="NLog.Web" />
</extensions>
<!-- define various log targets -->
<targets>
<!-- write logs to file -->
<!--<target xsi:type="File" name="allFile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />-->
<target xsi:type="File" name="allFile" fileName="c:\temp\nlog-all-${shortdate}.json">
<layout xsi:type="JsonLayout" includeAllProperties="false">
<attribute name="timeStamp" layout="${longdate}" />
<attribute name="sessionId" layout="${aspnet-request-ip}" />
<attribute name="level" layout="${level:upperCase=true}" />
<attribute name="source" layout="${logger}" />
<attribute name="message" layout="${message}" />
<attribute name="exception" layout="${exception}" />
</layout>
</target>
<!--<target xsi:type="File" name="ownFile" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />-->
<target xsi:type="File" name="ownFile" fileName="c:\temp\nlog-own-${shortdate}.json">
<layout xsi:type="JsonLayout" includeAllProperties="false">
<attribute name="timeStamp" layout="${longdate}" />
<attribute name="aspnet" layout="Hello ${aspnet-request-ip}" />
<attribute name="sessionId" layout="${aspnet-sessionid}" />
<attribute name="level" layout="${level:upperCase=true}" />
<attribute name="source" layout="${logger}" />
<attribute name="message" layout="${message}" />
<attribute name="exception" layout="${exception}" />
</layout>
</target>
<target xsi:type="Null" name="blackHole" />
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allFile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackHole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile" />
</rules>
</nlog>
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 20 (13 by maintainers)
where using
var service = GetService<IHttpContextAccessor>() var sessionid = service.HttpContext.Session.SessionID: