Use ASP.NET Query User for Log4net Log Entries

My ASP.NET site uses built-in authentication with impersonation disabled. I added "% username" to my "conversionPattern" in the web.config file to add a username to each registration entry. However, this will use the application pool identifier username rather than the current visitor name.

Any ideas on how best to do this? Should I write a custom appender? I do not mind minor penalties as this is a small intranet site. Thanks!

+3
source share
2 answers

An alternative (and simpler) solution that you might consider is to use a class ThreadContext(formerly sold as the MDCand NDCclasses) You can have something like this inside HttpModule(before the request will go to your page), or anywhere else before you register your first message:

ThreadContext.Properties["user"] = username;

And then include the following in your Pattern transformation:

%property{user}
+5
source

The latest versions of log4net have built-in ASP.NET template converters:

%aspnet-request{REMOTE_ADDR} and %aspnet-request{AUTH_USER}

fooobar.com/questions/389880 / ...

You can also access the content.

  • Cache
  • SAS context
  • SAS session

https://logging.apache.org/log4net/log4net-1.2.13/release/sdk/log4net.Layout.PatternLayout.html

+6
source

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


All Articles