Use the ASP.NET request user for Log4net log entries

雨燕双飞 提交于 2020-01-01 06:44:34

问题


My ASP.NET site is using Integrated Authentication with impersonation turned off. I have added a "%username" to my "conversionPattern" in the web.config to add the user name to each logging entry. However this will use the application pool identity's user name, and not the current visiting user's name.

Any ideas on how best to do this? Do I have to write a custom appender? I don't mind any minor performance penalties as this is a small intranet site. Thanks!


回答1:


An alternative (and easier) solution you may want to take a look at is to use the ThreadContext class (formerly implemented as the MDC and NDC classes). You could have something like this inside an HttpModule (before the request arrives at your page) or anywhere else before you log your first message:

ThreadContext.Properties["user"] = username;

And then include the following in your conversionPattern:

%property{user}



回答2:


There's built in ASP.NET pattern converters in recent versions of log4net:

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

https://stackoverflow.com/a/7788792/74585

You can also access the contents of

  • aspnet-cache
  • aspnet-context
  • aspnet-session

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



来源:https://stackoverflow.com/questions/938361/use-the-asp-net-request-user-for-log4net-log-entries

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