2012-08-07 62 views
2

所以我使用Tracesource登錄一些錯誤和wan't建立在用戶的本地Windows文件結構的日誌文件(像System.Environment.SpecialFolder.LocalApplicationData)。添加自定義路徑爲Tracesource TextWriterTraceListener會輸出配置

但是我不知道如果我可以做這樣的一個配置文件裏面什麼。

<system.diagnostics> 
    <trace autoflush="true"/> 
    <sources> 
     <source name="MainSource" 
       switchName="MainSwitch" 
       switchType="System.Diagnostics.SourceSwitch" > 
      <listeners> 
       <add name="LogFileListener" /> 
      </listeners> 
     </source> 
    </sources> 
    <sharedListeners> 
     <add name="LogFileListener" 
      type="System.Diagnostics.TextWriterTraceListener" 
      initializeData="This is the place the output file goes to" 
      traceOutputOptions="ProcessId, DateTime, Callstack" /> 
    </sharedListeners> 
    <switches> 
     <add name="MainSwitch" value="Verbose" /> 
    </switches> 
</system.diagnostics> 

initializeData是我認爲的一個參數的構造函數和是我將不得不把自定義路徑。

+0

這裏也是路徑如何相對解析微軟的代碼實現了更深入的探索:http://nicholas.piasecki.name/blog/2009/03/on-textwritertracelistener-inheritance-initializedata- ASPNET-和路徑/並且還一種替代方法示出該文章。 – JustAMartin 2012-11-19 13:40:47

回答

2

下面的示例代碼中,我使用我的選擇。它可以幫助你理解模式。

Configuration exeConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

ConfigurationSection diagnosticsSection = exeConfiguration.GetSection("system.diagnostics"); 

ConfigurationElementCollection switches = diagnosticsSection.ElementInformation 
                  .Properties["switches"] 
                  .Value as ConfigurationElementCollection; 

foreach (ConfigurationElement switchElement in switches) 
{ 
    Debug.WriteLine("switch: name=" + 
     switchElement.ElementInformation.Properties["name"].Value + 
     " value=" + 
     switchElement.ElementInformation.Properties["value"].Value); 
} 
+0

有自動的配置痕跡(通過配置)的可能性,考慮到我們是手工_opening_一個配置文件 - 就像我們在一個正常的.NET應用程序嗎? – deostroll 2015-11-03 05:47:16

相關問題