The Problem
A user (whether live or internal) allegedly has an issue with your ASP.NET web site that stores the user Id in the ASP.NET session. They describe the problem and the steps to reproduce, but even with that it would be nice to have more information about what specifically they did. Or better yet what the code did on their behalf.
For this, it helps to have more granular data than tracking. We will wade through the pile of application logs to find our smoking gun. If you’re using NLog odds are that you already have this.
Now there is a different problem, the sheer volume of log statements. Even for a relatively small site (~50 concurrent users) plucking out the relevant statements for your problem user becomes a problem.
The Solution
Simply add the userid or any other session variable to each log statement and then you can easily filter based on that. Wait a second though…I don’t want to have to edit each and every log statement. Fortunately thanks to NLog you don’t have to.
Install the NLog, NLog.Config, NLog.Schema and NLog.Web packages using the following commands.
NLog.Config will stand up a shell configuration file with examples.
NLog.Web will add the ability to use ASP.NET session variables and other goodies with NLog.
Update the NLog.Config file like below to include the new value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<targets> | |
<target name="dbLogger" xsi:type="Database" | |
connectionStringName="DefaultConnection" commandText=" | |
insert into dbo.Log ( | |
Date, Level, Message, Username, | |
ServerName, SessionId | |
) values ( | |
@Date, @Level, @Message, | |
@Username, @ServerName, @SessionId | |
);"> | |
<parameter layout="${Level}" name="@Level"/> | |
<parameter name="@username" layout="${identity}" /> | |
<parameter name="@Date" layout="${date}" /> | |
<parameter name="@Message" layout="${message}" /> | |
<parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" /> | |
<parameter name="@SessionId" layout="${aspnet-sessionid}" /> | |
<!– repeated –> | |
</target> | |
<!– | |
add your targets here | |
See https://github.com/nlog/NLog/wiki/Targets for possible targets. | |
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers. | |
–> | |
<!– | |
Write events to a file with the date in the filename. | |
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" | |
layout="${longdate} ${uppercase:${level}} ${message}" /> | |
–> | |
</targets> |
https://gist.github.com/ryanvgates/daff6e67ae0ebe05637b6fb64a9cc506#file-nlog-config
There you have it. Now you can easily filter log entries by user. You can find my code here.