我正在尝试通过在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}
" 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 & Warnings" />
</specialSources>
</loggingConfiguration>
</configuration>
我希望它只在日志文件中附加一行:
消息:示例日志文本
但它增加了以下内容:
消息:类别“信息”没有明确的映射。该 日志条目是:时间戳:31/10/2013 8:59:01 AM
消息:示例日志文本
类别:信息
优先级:2
EventId:1
严重性:信息
名称:
机器:SERVER1
App Domain:SAMPLE.exe
ProcessId:1104
流程名称:
主题名称:
Win32 ThreadId:9644
扩展属性:
如何摆脱额外的线路?
使用的API是:
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(message, category, priority)
答案 0 :(得分: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" />
困难在于找到我有兴趣添加到日志中的数据的模板关键字。