问题
I'm working with Nlog logging framework. currently I'm facing issue, that unable to save aspnet-sessionid.
<target xsi:type="File"name="LogFileAppender"fileName="C:\Users\acer\Downloads\test1.txt"
layout="${longdate} ${aspnet-sessionid} - ${message} "/>
It is saving (as per layout) in target fileName like
2020-05-07 11:16:56.4047 - Global.asax.cs:Application_Start(): Application initializing, starting cache load
But in log4net I'm able to save aspnet-sessionid
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="C:\Users\acer\Downloads\test1.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyy-MM-dd.log"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %aspnet-session{SessionID} - %message"/>
</layout>
It is saving (as per conversion pattern) in target fileName like
2020-05-06 12:09:49,560 26aef5e9-16-132332207751900447spnet-session{SessionID} - Global.asax.cs:Application_Start(): Application initializing, starting cache load
The problem is, with log4net logging framework I'm able to save aspnet-sessionid but not with NLog framework.Any help is welcome.
Thanks.
回答1:
This NLog ${aspnet-sessionid}
renders the following:
- HttpContext.Session.SessionID
This Log4net %aspnet-session{SessionID}
renders the following:
- HttpContext.Session.Content["SessionID"]
It is matching this NLog LayoutRenderer: ${aspnet-session:SessionID}
See also https://github.com/nlog/nlog/wiki/AspNetSession-Layout-Renderer
回答2:
It's mistake with log4net Conversion pattern only.
<conversionPattern value="%date %aspnet-session{SessionID} - %message"/>
In this conversionpattern, for %aspnet-session{SessionID} in log4net it will consider only %a and it will be appdomain conversion pattern.
So , its in NLog will be like
<target xsi:type="File"name="LogFileAppender"fileName="C:\Users\acer\Downloads\test1.txt"
layout="${longdate} ${appdomain} - ${message} "/>
So, its confusion with name of conversion pattern for aspnet-sessionid in log4net.
[https://github.com/NLog/NLog/wiki/AppDomain-Layout-Renderer] refered for appdomain layout renderer.
来源:https://stackoverflow.com/questions/61673133/unable-to-save-aspnet-sessionid-using-nlog