![](/img/trans.png)
[英]How to Log Client Ip, Browser Name and User Name using Log4net in asp.net when multiple clients are using application at same time?
[英]How to Log Client Ip, Browser Name and User Name using Log4net in asp.net?
我想使用log4net库记录客户端IP地址,浏览器名称和Windows身份验证用户名。
我正在尝试以这种方式,但无法正常工作。
所以请帮帮我。
主要问题是我不知道如何在log4net中添加多个自定义属性。
请帮我。
服务器端代码。
log4net.GlobalContext.Properties["Hostname"] = GetIP();
log4net.GlobalContext.Properties["Browser"] = HttpContext.Current.Request.Browser.Type;
log4net.GlobalContext.Properties["username"] = HttpContext.Current.Request.LogonUserIdentity.Name;
Web.Config设置:
<conversionPattern value="%date %property{Hostname, username, Browser} [%thread]
 %-5level %logger - %message%newline"
/>
如果需要其他设置,请告诉我。
或还有其他方法可以执行此操作,请让我知道。
为您提供一个更“优雅”的解决方案:使用ndc。
<conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
在代码(NDC)中使用嵌套诊断上下文 :
using(log4net.NDC.Push("["+GetIP() + " - " + HttpContext.Current.Request.Browser.Type + " - " + HttpContext.Current.Request.LogonUserIdentity.Name+"]")
{
//your code
//all log messages will contain the ndc string with the using.
}
可以通过这种方式完成,对其他人有用。
<conversionPattern value="%date [%property{Hostname}] [%property{UserName}] [%property{Browser}] [%property{WindowsLogin}] [%thread]
 %-5level %logger - %message%newline"
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.