【问题标题】:how to add some properties to log entry如何将一些属性添加到日志条目
【发布时间】:2011-01-29 07:49:58
【问题描述】:

我使用 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();

【问题讨论】:

  • @Tuzo,感谢您提供的信息!我怀疑该特定问题会有一个片段 ID,但没有花时间查找它。下次我会使用直接超链接。

标签: c# asp.net enterprise-library logging-application-block


【解决方案1】:

此代码未经测试

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);

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多