【问题标题】:Why its not following template from formatter为什么它不遵循格式化程序中的模板
【发布时间】:2011-03-04 19:44:51
【问题描述】:

这是我的配置文件

<?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="" tracingEnabled="true" defaultCategory="General" >
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="Enterprise Library Logging" formatter="Text Formatter"
                log="" machineName="." traceOutputOptions="None" />
      <add name="Rolling Flat File Trace Listener" 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="F:\MyLogFile.log" footer="" header="" rollInterval="Hour"
                traceOutputOptions="None" formatter="Text Formatter" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="{timestamp} {severity} {message} "
                name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>

当我使用下面的代码时

 Logger.Write("hello world", "", 0, 0, TraceEventType.Information);

我在日志文件中得到以下信息

2011 年 3 月 4 日下午 7:40:26 错误没有明确映射类别“”。日志条目是:
时间戳:2011 年 3 月 4 日晚上 7:40:26
消息:Hello World
分类:
优先级:0
事件 ID:0
严重性:信息
标题:
机器:MyPC
应用域:ConsoleApplication1.vshost.exe
进程 ID:8912
进程名称:C:\ConsoleApplication\bin\Debug\ConsoleApplication.vshost.exe
主题名称:
Win32 线程 ID:5496
扩展属性:

我做错了什么使它不尊重我在格式化程序中定义的模板=“{timestamp} {severity} {message}”。

【问题讨论】:

  • 你使用什么记录器?并显示所有配置文件
  • 企业库日志记录块。您在配置文件中看到了什么?

标签: c# enterprise-library


【解决方案1】:

您已经使用名称“General”定义了您的类别,但是当您登录时使用的是“”类别。

Logger.Write("hello world", "", 0, 0, TraceEventType.Information);

因此,您的 LogEntry 不是由您的“常规”类别处理,而是由 specialSourcenotProcessed 处理。这就是“没有明确的类别映射”消息试图告诉您的内容。

要使用您的类别,请将名称传递给 Write 方法:

Logger.Write("hello world", "General", 0, 0, TraceEventType.Information);

或不指定类别,因为“常规”被定义为默认类别。

【讨论】:

  • 谢谢,但是现在我收到“尝试获取 LogWriter 类型的实例时发生激活错误,键“”“错误。不确定这是否可能是由于在 dll 中配置了企业库日志记录块。不过,我已经发布了一个单独的问题。
猜你喜欢
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
相关资源
最近更新 更多