Track user with log4net for website

I am looking for the best way for a user to track on an asp.net based website.
We use log4net to register some business activities (enter on this page, click a button, etc.). But for several users, the log file cannot be read easily.

Therefore, I need to add the "UserName" property in the configuration file as follows:

<conversionPattern value="%date [%thread] - %property{UserName} - %-5level - %logger - %message%newline"/> 

Do you have an idea on how to set "UserName"?

thanks for the help

+4
source share
2 answers

The following property is

  %username the Windows identity of user making the log entry; slow 

check in more detail: http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx

check this article: Capturing web events in log4net

0
source

You can define a username (or any other), for example:

 this.SetThreadContext("UserName", userName); 

Assuming userName contains the username.

Edit (what you really want to do):

Write a wrapper for log4net (with an interface) and make some method, for example. SetUserName as follows:

 public void SetUserName(Func<string> getUserName) 

You call this method when the application starts, and you pass some method that returns the current user (from the HttpContext). You will need to store the delegate somewhere, maybe a static variable. Then you have methods for logging at different levels, and each method calls a delegate ( Func<string> getUserName ) and sets the thread context property, as shown above. Thus, you do not need to set the context property of the stream manually before each call to the registrar ...

0
source

Source: https://habr.com/ru/post/1312344/


All Articles