【问题标题】:Prevent Enterprise library logging from adding default values to log防止企业库日志记录将默认值添加到日志中
【发布时间】:2013-10-31 09:33:37
【问题描述】:

我正在尝试通过在 app.config 文件中添加格式化程序来自定义企业库日志记录。问题是日志 API 会转储附加信息以及我在格式化程序中指定的项目。

这里是 App.config 文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <loggingConfiguration name="LoggingBlock" tracingEnabled="true"
    defaultCategory="General">
    <listeners>
      <add name="RollingFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="DataLog.log" formatter="SimpleFormatter" rollFileExistsBehavior="Increment"
        rollInterval="Day" rollSizeKB="10000" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Message: {message}{newline}&#xA;" name="SimpleFormatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="RollingFile" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>
  </loggingConfiguration>
</configuration>

我希望它只在日志文件中附加一行:

消息:示例日志文本

但它添加了以下内容:

消息:“信息”类别没有明确的映射。这 日志条目是:时间戳:31/10/2013 8:59:01 AM

消息:示例日志文本

类别:信息

优先级:2

事件 ID:1

严重性:信息

标题:

机器:SERVER1

应用域:SAMPLE.exe

进程 ID:1104

进程名称:

线程名称:

Win32 线程 ID:9644

扩展属性:

如何去掉多余的行?

使用的API是:

Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(message, category, priority)

【问题讨论】:

    标签: logging enterprise-library-5


    【解决方案1】:

    最后发现你可以通过覆盖 app.config 文件中的格式化程序类型来做到这一点,如下所示:

    <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            template="[{timestamp(local)}] {category},{machine},{message}"
            name="Text Formatter" />
    

    困难在于为我有兴趣添加到日志中的数据找到模板关键字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多