1

我正在尝试通过在 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

事件编号:1

严重性:信息

标题:

机器:SERVER1

应用程序域:SAMPLE.exe

进程号:1104

进程名称:

线程名称:

Win32 线程 ID:9644

扩展属性:

如何摆脱额外的行?

使用的 API 是:

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

1 回答 1

0

最后发现您可以通过覆盖 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" />

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

于 2014-03-14T02:19:51.790 回答