使用log4net的日志条目ASP.NET请求用户 [英] Use the ASP.NET request user for Log4net log entries

查看:82
本文介绍了使用log4net的日志条目ASP.NET请求用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.NET网站使用集成身份验证与模拟关闭。我在web.config中增加了一个%USERNAME我的conversionPattern用户名添加到每个日志条目。然而,这将使用的应用程序池标识的用户名,而不是当前访问用户的名称。

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.

对如何最好地做到这一点任何想法?我必须编写自定义的appender?我不介意任何轻微的性能损失,因为这是一个很小的内部网站上。谢谢!

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!

推荐答案

这是另一种(更容易)解决方案,你可能想看看是使用的 ThreadContext 类(以前为的 MDC 和的 NDC 类)。你可能有这样的事情内部的的HttpModule (请求到达你的页面之前)或登录你的第一个消息之前,其他地方:

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;

然后在您conversionPattern以下内容:

And then include the following in your conversionPattern:

%property{user}

这篇关于使用log4net的日志条目ASP.NET请求用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆