我想使用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.
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句