Unable to save aspnet-sessionid using NLog

非 Y 不嫁゛ 提交于 2020-05-24 06:08:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!