我使用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();
答案 0 :(得分: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);
答案 1 :(得分:1)
您可以直接以字符串Logger.Write(“”)编写,也可以创建自己的LogEntry类并从LogEntry继承,然后更改配置中的Formatters以包含新属性。
重新阅读您的问题只需使用扩展属性,然后更改配置中的格式化程序。