Microsoft企业库日志记录应用程序块格式不正确

时间:2013-12-11 02:31:46

标签: c# enterprise-library enterprise-library-5

我们在我们的网站上使用MS Enterprise Library 5.0 Logging。

当异常未处理时,我们希望将其与一条上下文信息一起记录,以帮助调试问题。因此,我们有一个HttpModule位于网站顶部,其工作是捕获这些并将它们记录到Windows应用程序事件日志中。这都是非常香草的东西。

它可以在我们的生产网络服务器上运行。它在我们的测试Web服务器上无法正常工作。

问题是LogEvent.ExtendedProperties集合未正确呈现(或根本不呈现)。相反,替换令牌只是逐字地转储到事件日志中。

生产事件日志显示这样的信息(稍微缩写),所有内容都按照您期望的方式格式化:

12/10/2013 06:07:13 PM
LogName=Application
SourceName=Secure Website
EventCode=1729
EventType=2
Type=Error
ComputerName=WSSECURE09.website.nordstrom.com
TaskCategory=%1
OpCode=Info
RecordNumber=220338
Keywords=Classic
Message=Message: Unhandled exception in Secure Website: System.ServiceModel.FaultException`1[Nordstrom.Contracts.Fault.ServiceFault]:
  Argument shopper is invalid.
  Email is null or empty.
  (Fault Detail is equal to Error code: InvalidArguments).
.
.
.
Extended Properties: Shopper ID - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

最后一行(Extended Properties: ...)转储ExtendedProperties集合并提供遇到问题的购物者的ID以帮助调试它。在我们的测试环境中,扩展属性集合根本不会被转储。相反,模板中的原始标记是逐字写出的,没有执行替换:

Extended Properties: {key} - {value}
)}

这是正在使用的格式化程序模板,直接来自web.config文件。我为可读性添加了换行符,但这是唯一的变化:

<formatters>
  <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
    template="
      Message: {message}&#xA;
      Category: {category}&#xA;
      Priority: {priority}&#xA;
      EventId: {eventid}&#xA;
      Severity: {severity}&#xA;
      Title:{title}&#xA;
      Machine: {machine}&#xA;
      Application Domain: {appDomain}&#xA;
      Process Id: {processId}&#xA;
      Process Name: {processName}&#xA;
      Win32 Thread Id: {win32ThreadId}&#xA;
      Thread Name: {threadName}&#xA;
      Extended Properties: {dictionary({key} - {value}&#xA;)}
      "
    name="Text Formatter"
    />
</formatters>

这是[稍微清理过]日志记录代码:

private static void LogHandledException( Exception ex )
{
  HttpContext context = HttpContext.Current ;
  LogEntry    entry   = new LogEntry() ;

  entry.Categories.Add("Errors");
  entry.Priority = 1;
  entry.Severity = TraceEventType.Critical;
  entry.Message  = string.Format( "Unhandled exception in Secure Website: {0}" , ex ) ;
  entry.EventId  = (int) EventId.UnhandledException ;

  // Add shopper id to the log, if we can find it in the http context
  Shopper shopper = HttpContext.Current.Items[ "shopper" ] as Shopper ;
  if ( shopper != null )
  {
    bool hasShopperId = ! string.IsNullOrWhiteSpace( shopper.Id ) ;
    if ( hasShopperId )
    {
      KeyValuePair<string,object> item = new KeyValuePair<string,object>("ShopperID",shopper.Id) ;
      entry.ExtendedProperties.Add( item ) ;
    }
  }

  Logger.Write(entry);
  return ;
}

1 个答案:

答案 0 :(得分:1)

我们的配置管理团队已经推出了自己的工具来进行配置文件转换:

该工具的解析器显然是......写得不正确。嵌套的花括号似乎混淆了它。为什么它会在字符串文字中进行解析,或者为什么它可以适用于我们的生产环境转换,但不适用于任何其他环境的转换,我不知道,但CM团队修复了他们的工具。 Et瞧!问题解决了。

我没有想到我们的构建工具会被破坏(或者我们已经推出了我们自己的东西以获得可用于完成工作的相当标准的东西),但是一旦我排除了我们的代码,并排除了不同服务器上的配置差异,它没有为其他任何东西留下太多空间。正如Sherlock Holmes所说,

  

“我经常告诉你,当你消除了不可能的事情,无论如何   遗骸,无论多么不可能,都必须是真理?我们知道他没有通过   门,窗户或烟囱。我们也知道他不可能   隐藏在房间里,因为没有隐藏的可能性。那么,他什么时候来?“   
- 夏洛克·福尔摩斯在亚瑟·柯南·道尔爵士的四人的标志