2011-02-11 40 views
0

我正在使用Ent Lib 5.我有一個事件日誌偵聽器,其Source屬性當前設置爲我們的應用程序套件的通用名稱。在運行時更改偵聽器屬性

此配置文件與多個項目鏈接/共享(長篇故事,不會更改)。

我們仍然希望每個應用程序都將其自己的源名稱放入事件日誌中以唯一標識它......如何在運行時更改源名稱?

我可以輕鬆地更改我想要的關於實際日誌事件本身的任何內容,但我想更改每個應用程序的偵聽器源屬性。

希望這是有道理的

UPDATE: 下面是我們所使用的配置設置......這是我想在運行時更改源屬性。

添加名稱= 「格式化事件日誌常規」 TYPE = 「Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener,Microsoft.Practices.EnterpriseLibrary.Logging,版本= 5.0.0.0,文化=中性公鑰= 31bf3856ad364e35」 listenerDataType = 「Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData,Microsoft.Practices.EnterpriseLibrary.Logging,版本= 5.0.0.0,文化=中性公鑰= 31bf3856ad364e35」 格式= 「文本格式化」 traceOutputOptions = 「無」 過濾器=」所有「machineName =」。「 source =「在運行時更改」log =「應用程序」

回答

0

您可以要求Logger寫入特定類別。

下面的示例創建了2個類別 - AppOne,AppTwo。 然後我添加了兩個跟蹤偵聽器--AppOneListener(綁定到AppOne類別)和AppTwoListener(綁定到AppTwo偵聽器)。

而在源代碼中,當你想記錄時,指定類別。

Logger.Write("test 1", "AppTwo"); 

下面是配置: 看那categorySources部分和聽衆節。

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="AppOne"> 
     <listeners> 
      <add name="AppOneListener" 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="traceAppOne.log" formatter="Text Formatter" /> 
      <add name="AppTwoListener" 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="traceAppTwo.log" 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="Title:{title}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;" 
       name="Text Formatter" /> 
     </formatters> 
     <categorySources> 
      <add switchValue="All" name="AppOne" /> 
      <add switchValue="All" name="AppTwo"> 
       <listeners> 
        <add name="AppTwoListener" /> 
       </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> 
+0

對不起,我想我沒有解釋得很清楚。我們使用事件日誌監聽器,它具有與平面文件不同的屬性。我們只想要一個聽衆。我們目前有31個應用程序使用相同的鏈接配置文件。我們不希望只有31個偵聽器,只有源名稱不同。它使配置文件變大(我們有很多其他東西在那裏)。 – 2011-02-11 06:55:05