1

我使用 Microsoft Enterprise Library 5.0(记录应用程序块)。我想将 IP 地址和用户名保存到数据库中。如何将这两列添加到 Logging Application Block?

HttpContext _Context = HttpContext.Current;
Exception _ex = _Context.Server.GetLastError();
LogEntry _LogEntery = new LogEntry();
if (_ex.InnerException != null)
{
    _LogEntery.Message = _ex.InnerException.ToString();
}
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
    _ex.Data.Add("UserName", HttpContext.Current.User.Identity.Name);

}
_ex.Data.Add("IPaddress", Request.UserHostAddress);
_LogEntery.Title = _ex.Message.ToString();
_LogEntery.ExtendedProperties.Add("Ip", _ex.Data["IPaddress"]);
_LogEntery.Categories.Add("Database");
Logger.Write(_LogEntery);
_Context.Server.ClearError();
4

2 回答 2

2

此代码未经测试

IDictionary *contextInfo* = new Hashtable();

   contextInfo.Add("Additional Info", "Some information I wanted logged");

   DebugInformationProvider provider = new DebugInformationProvider();
   provider.PopulateDictionary(contextInfo);

   LogEntry logEntry           = new LogEntry();
   logEntry.Message            = "Logged with context specific information";
   logEntry.ExtendedProperties = *contextInfo*;

   Logger.Write(logEntry);
于 2012-11-14T10:23:22.397 回答
1

您可以直接写为字符串 Logger.Write("") 或者您可以创建自己的 LogEntry 类并从 LogEntry 继承,然后更改配置中的格式化程序以包含您的新属性。

重新阅读您的问题只需使用扩展属性,然后更改配置中的格式化程序。

于 2011-01-31T20:04:24.613 回答