【问题标题】:How to customize the formatter for the loggingConfiguration?如何自定义 loggingConfiguration 的格式化程序?
【发布时间】:2012-12-09 10:20:09
【问题描述】:

我是 EnterpriseLibrary.Logging 的新手。在 web.config 中,在 loggingConfiguration 下,我们有以下格式化程序:

<formatters>
    <add name="Default Formatter" template="&#xA;Timestamp: {timestamp(local)}&#xA;&#xA;Message: {message}&#xA;&#xA;Category: {category}&#xA;&#xA;Priority: {priority}&#xA;&#xA;EventId: {eventid}&#xA;&#xA;Severity: {severity}&#xA;&#xA;Title:{title}&#xA;&#xA;Machine: {machine}&#xA;&#xA;Application Domain: {appDomain}&#xA;&#xA;Process Id: {processId}&#xA;&#xA;Process Name: {processName}&#xA;&#xA;Win32 Thread Id: {win32ThreadId}&#xA;&#xA;Thread Name: {threadName}&#xA;&#xA;User Name: {userName}&#xA;&#xA; extended Properties: {dictionary({key} - {value}&#xA;)}" 
             type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    <add name="DBA Formatter" template="&#xA;Timestamp: {timestamp(local)}&#xA;&#xA;Message: {message}&#xA;&#xA;Category: {category}&#xA;&#xA;Priority: {priority}&#xA;&#xA;EventId: {eventid}&#xA;&#xA;Severity: {severity}&#xA;&#xA;Title:{title}&#xA;&#xA;Machine: {machine}&#xA;&#xA;Extended Properties: {dictionary({key} - {value}&#xA;)}" 
             type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</formatters>

在后面的代码中,我们只设置了消息到LogEntry。我想知道其他参数是如何设置的,例如时间戳、类别、优先级等。我添加了用户名,但不起作用。有人能帮我解释一下吗?

【问题讨论】:

    标签: asp.net logging exception-handling enterprise-library


    【解决方案1】:

    这是有关这些参数的 Microsoft 文章:Configuring Formatters。它讨论了您可以在格式化程序中插入的各种标记。

    “用户名”不是预定义的标记之一。但是您可以(可能)使用 LogEntry 的 ExtendedProperties 属性来解决这个问题:

     LogEntry le = new LogEntry();
     le.ExtendedProperties.Add("username", "jsmith");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 2017-03-23
      相关资源
      最近更新 更多