0

我已启用两个使用企业库的跟踪侦听器来登录到事件日志和日志文件。以编程方式打开到企业库中的跟踪侦听器

是否可以选择在任何时间点上以编程方式登录到一个日志(例如我想记录一些事件日志和记录文件一些时间)。

<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="Enterpriselibrary" formatter="Text Formatter" log="TestLog" 
     machineName="." traceOutputOptions="None" /> 
     <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
     fileName="C:\AER\test.log" formatter="Text Formatter" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" /> 
    </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: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}" 
     name="Text Formatter" /> 
    </formatters> 
    <categorySources> 
     <add switchValue="All" name="General"> 
     <listeners> 
      <add name="Event Log Listener" /> 
      <add name="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="Event Log Listener" /> 
     </listeners> 
     </errors> 
    </specialSources> 
    </loggingConfiguration> 
+0

任何帮助表示赞赏..请帮助... – user2020710 2013-03-13 04:55:38

回答

2

当然,刚刚创建单独的类别和准个人跟踪侦听器与每个类别。然后,在您的代码中,您只需指定您希望LogEntry登录的类别。像这样:

定义你的类别:

<categorySources> 
    <add switchValue="All" name="Category"> 
    <listeners> 
     <add name="Event Log Listener" /> 
    </listeners> 
    </add> 
    <add switchValue="All" name="AnotherCategory"> 
    <listeners> 
     <add name="Flat File Trace Listener" /> 
    </listeners> 
    </add> 
</categorySources> 

,然后以编程引导你的日志条目:

LogWriter.Write("Log entry through a specific Category to the Event Log", "Category"); 
LogWriter.Write("Log entry through another Category to a file", "AnotherCategory"); 
相关问题