| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
ASP.NET Item Variable from Session Dictionary
Platforms Supported: All
${aspnet-session:item=String:objectpath=String:format=String:ValueType=String|Int32}
Introduced with NLog.Web v5.1, and replaces the Variable-option
Introduced with NLog.Web v5.0
Introduced with NLog.Web v5.0
Introduced with NLog.Web v5.2 and replaces EvaluateAsNestedProperties-option
Introduced in NLog.Web.AspNetCore 4.9.3. Please note: you won't get an error when retrieving a int (set with Session.SetInt32(..)) with valuetype string, but an incorrect value. We can't unfortunately recognize the type of the value inside the ASP.NET Core session.
See also Configure session state
Log the username in the session.
In the C# code:
Session["username"] = "JohnDoe";Config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" xsi:type="File" fileName="file.txt" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" layout="${aspnet-session:item=username}" />
</rules>
</nlog>Will print "JohnDoe"
In the C# code:
Session["user"] = new UserInfo { Name= "JohnDoe", Id = 100};Config:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
<targets>
<target name="logfile" xsi:type="File" fileName="file.txt" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" layout="${aspnet-session:item=user:ObjectPath=Name}" />
</rules>
</nlog>Will print nested property-value "JohnDoe"
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
| Back | FazBrowse Home | New Git URL |